Python Django的URL设置
一、url使用方式
1、正则表达方式:
url(r‘^blog/index/$‘,‘blog.views.index‘),
2、导入的方式:
from blog.views import index
.....
url(r‘^blog/index/$‘,index),
.....
3、
urlpatterns = patterns(‘blog.views‘,
url(r‘^blog/index/$‘,‘index‘),
)
二、URL参数传递给index
添加template目录:
# vim setting.py
TEMPLATE_DIRS = (
"blog/template",
)
关键字传递
url(r‘^blog/index/(?P<id>\d{2})/$‘,‘blog.views.index‘),
# cat blog/views.py
from django.shortcuts import render_to_response
def index(req,id):
user = {‘name‘:‘tom‘,‘age‘:23,‘sex‘:‘male‘}
book_list = [‘python‘,‘java‘,‘php‘,‘web‘]
return render_to_response(‘index.html‘,{‘title‘:‘Loyu page‘,‘book_list‘:book_list,‘user‘:user,‘id‘:id,‘text‘:‘I LOVE XIAOXIAO‘})
未定义参数
url(r‘^blog/index/(\d{2})/$‘,‘blog.views.index‘),
# cat blog/views.py
from django.shortcuts import render_to_response
def index(req,loyu):
user = {‘name‘:‘tom‘,‘age‘:23,‘sex‘:‘male‘}
book_list = [‘python‘,‘java‘,‘php‘,‘web‘]
return render_to_response(‘index.html‘,{‘title‘:‘Loyu page‘,‘book_list‘:book_list,‘user‘:user,‘id‘:loyu,‘text‘:‘I LOVE XIAOXIAO‘})
# cat blog/template/index.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0.5 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>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>{{title}}</title>
</head>
<body>
{{text}}
{{id}}
</body>
</html>
本文出自 “流星宇” 博客,请务必保留此出处http://8789878.blog.51cto.com/8779878/1850738
原文地址:http://8789878.blog.51cto.com/8779878/1850738