Differences between revisions 2 and 7 (spanning 5 versions)
Revision 2 as of 2009-10-01 14:49:05
Size: 1250
Editor: askfor
Comment:
Revision 7 as of 2009-12-25 07:17:11
Size: 1318
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 18: Line 18:
    url(r'^$', direct_to_template,{'template': 'hello.html', 'extra_context':{'name':'world'}}),     url(r'^$', direct_to_template,{'template': 'hello.html', 'extra_context':{'name':'template'}}),
Line 36: Line 36:
保存hello.html放在前面配置的templates目录中
name
是extra_context传来的
params.name
是'^(?P<name>[\w]+)/$' 匹配到的
{% firstof params.name name %} params.name有值就取params.name,没有继续看name是否有值
保存hello.html放在前面配置的templates目录中    {{ name }}是extra_context传来的    {{ params.name }}是'^(?P<name>[\w]+)/$' 匹配到后django自动加入

{% firstof params.name name %} params.name有值就取params.name,没有继续看name是否有值   
Line 43: Line 46:
在浏览器中访问
http://localhost:8000/
http://localhost:8000/world/
http://localhost:8000/askfor/
在浏览器中访问   
http://localhost:8000/   
http://localhost:8000/world/   
http://localhost:8000/django/  

创建工程

django-admin.py startproject helloworld

修改settings 设置模板目录

import os
ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
    os.path.join(ROOT_PATH, 'templates')
)

在根目录下创建templates文件夹

修改urls.py

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
    url(r'^$', direct_to_template,{'template': 'hello.html', 'extra_context':{'name':'template'}}),
    url(r'^(?P<name>[\w]+)/$', direct_to_template,{'template': 'hello.html'}),
)

创建模板

<html>
<head>
<title>django helloworld</title>
</head>
<body>
<h1>hello,{{ name }}</h1>
<h1>hello,{{ params.name }}</h1>
<h1>hello,{% firstof params.name name %}</h1>
</bodY>
</html>

保存为hello.html放在前面配置的templates目录中

name是extra_context传来的

params.name是'^(?P<name>[\w]+)/$' 匹配到后django自动加入的。

{% firstof params.name name %} params.name有值就取params.name,没有继续看name是否有值

运行

在helloworld目录下 manage.py runserver 8000 在浏览器中访问

http://localhost:8000/

http://localhost:8000/world/

http://localhost:8000/django/

DjangoHelloGenericViews (last edited 2009-12-25 07:17:11 by localhost)