Django项目3
1.先做准备工作[plain]
django-admin.py startproject web03
django-admin.py startapp blog
python manage.py runserver
在settings.py里加上'blog'
2.配置url
[python]
#coding:utf8
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
)
urlpatterns += patterns('', #这里两种方式等价
url(r'^blog/index/(\w.*)/(\d{1,2})/$','blog.views.index'), #位置参数形式
url(r'^blog/show/(?P<uname>\w.*)/(?P<age>\d{1,2})/$','blog.views.index'),#关键字方式调用
url(r'^blog/disp_new/$','blog.views.disp',{'template_name':'new.html'}), #默认参数形式。在后面加同一个字典对应不同的模板
url(r'^blog/disp_story/$','blog.views.disp',{'template_name':'story.html'}),#这样就不必写多个视图处理方法
url(r'^blog/disp_arm/$','blog.views.disp',{'template_name':'arm.html'}),
)
urlpatterns += patterns('',
url(r'^show1/$','blog.views.show1'), #匹配的优先级,上面比下面要高(重复冲突情况下)
url(r'^show1/$','blog.views.show2'),
)
3.配置view
[python]
#coding:utf8
from django.http import HttpResponse
from django.template import loader,Context
def index(req,uname,age): #加了个uname,加了个对应关系,从url那个组里传过来的
t=loader.get_template('index.html')
c=Context({
'uname':uname,
'age':age,
})
return HttpResponse(t.render(c))
def disp(req,template_name): #url中有三个模板,一个template_name就搞定了哦
t=loader.get_template(template_name) #这里注意,参数不加引号哦
c=Context({
})
return HttpResponse(t.render(c))
def show1(req):
return HttpResponse('show1')
def show2(req):
return HttpResponse('show2')
4.配置templates
[html]
<!-- index .html -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> www.zzzyk.com
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
name is :{{uname}}<br />
age is :{{age}}
</body>
</html>
<!-- new.html ,此处省略-->
<!-- story.html ,此处省略-->
<!-- arm.html ,此处省略-->
补充:Web开发 , Python ,