09/03 24

django 学习笔记 (一) 不指定

jasonyu , 18:01 , 我的收藏 » Other , 评论(0) , 引用(0) , 阅读(995) , Via 本站原创 | |
学习资料: http://djangobook.py3k.cn

安装好python,下载安装 MySQLdb, 下载好django, 并且进入django目录
sudo python setup.py install


到项目根目录下
django-admin.py startproject mysite

p.s. : mysite为项目名

进入项目执行下面命令,启动开发服务器
python manage.py runserver


python manage.py runserver 8080


访问 http://127.0.0.1:8000/


-----------------------------------------------
视图:
进入项目,新建views.py

将 URL 映射到视图, 编辑 urls.py

Django如何处理请求: 完整细节 >>>



-------------------------------------------------
Django模板系统
>>> from django.template import Context, Template
>>> t = Template("My name is {{ name }}.")
>>> c = Context({"name": "Stephane"})
>>> t.render(c) 'My name is Stephane.'


标签
if/else  
{% if today_is_weekend %}
    <p>Welcome to the weekend!</p>
{% else %}
    <p>Get back to work.</p>
{% endif %}


for  
{% for country in countries %}
    <table>
    {% for city in country.city_list %}
        <tr>
        <td>Country #{{ forloop.parentloop.counter }}</td>
        <td>City #{{ forloop.counter }}</td>
        <td>{{ city }}</td>
        </tr>
    {% endfor %}
    </table>
{% endfor %}


ifequal/ifnotequal
{% ifequal section 'sitenews' %}
    <h1>Site News</h1>
{% else %}
    <h1>No News Here</h1>
{% endifequal %}


注释
{# This is a comment #}


过滤器
{{ pub_date|date:"F j, Y" }}


render_to_response()
from django.shortcuts import render_to_response
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('current_datetime.html', {'current_date': now})


模板继承
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <h1>My helpful timestamp site</h1>
    {% block content %}{% endblock %}
    {% block footer %}
    <hr>
    <p>Thanks for visiting my site.</p>
    {% endblock %}
</body>
</html>


{% extends "base.html" %}

{% block title %}The current time{% endblock %}

{% block content %}
<p>It is now {{ current_date }}.</p>
{% endblock %}
Tags: ,
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]