09/03
24
学习资料: http://djangobook.py3k.cn
安装好python,下载安装 MySQLdb, 下载好django, 并且进入django目录
到项目根目录下
p.s. : mysite为项目名
进入项目执行下面命令,启动开发服务器
或
访问 http://127.0.0.1:8000/
-----------------------------------------------
视图:
进入项目,新建views.py
将 URL 映射到视图, 编辑 urls.py
Django如何处理请求: 完整细节 >>>
-------------------------------------------------
Django模板系统
标签
if/else
for
ifequal/ifnotequal
注释
过滤器
render_to_response()
模板继承
安装好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.'
>>> 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 %}
<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 %}
<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 %}
<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})
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>
<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 %}
{% block title %}The current time{% endblock %}
{% block content %}
<p>It is now {{ current_date }}.</p>
{% endblock %}
Installing Zope and
未来几年动态语言谁将成为主流


