Python Django的URL設置php
1、url使用方式html
一、正則表達方式: java
url(r'^blog/index/$','blog.views.index'),python
二、導入的方式: web
from blog.views import indexdjango
.....vim
url(r'^blog/index/$',index),ide
.....ui
三、url
urlpatterns = patterns('blog.views',
url(r'^blog/index/$','index'),
)
2、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>