django 的 url 配置主要在 urls.py 中进行
urlconfig 中对 url 的处理方式主要在:
一 视图处理方式
如 上文 例子所示:
url(r'^blog/index/$', 'blog.views.index'),
二 直接用方法名代表 url 处理方式:
from django.conf.urls import patterns, include, url from hi.views import index # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'csvt01.views.home', name='home'), # url(r'^csvt01/', include('csvt01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), #url(r'^hi/index/$', 'hi.views.index'), url(r'^hi/index/$', index), )
三 统一处理模块名的前缀
url 视图模块的前缀可以统一省略,patterns() 中的第一个参数即为前缀名:
from django.conf.urls import patterns, include, url from hi.views import index # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('hi.views', # Examples: # url(r'^$', 'csvt01.views.home', name='home'), # url(r'^csvt01/', include('csvt01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^hi/index/$', 'index'), #url(r'^hi/index/$', index), )
四 url 中传递参数
urls.py:
from django.conf.urls import patterns, include, url from hi.views import index # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('hi.views', # Examples: # url(r'^$', 'csvt01.views.home', name='home'), # url(r'^csvt01/', include('csvt01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^hi/index/(?P<id>d*)/$', 'index'), #url(r'^hi/index/$', index), )
views.py:
from django.shortcuts import render_to_response class Person(object): def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def say(self): return "This is " + self.name def index(req, id): books = {'a':'linux in a nutshell','b':'unix programming'} user = Person('eli', 24, 'male') user_list = ['eli', 'lie', 'iel'] return render_to_response('index.html', {'title':'Django Sample', 'context':user, 'users':user_list, 'books':books, 'id':id})
则形如 127.0.0.1:8000/blog/index/4893/ 的地址中的 4893 将会通过正则表达式分组传递给 index.html:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>{{title}}</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="keywords" content="Django Template" /> <meta name="description" content="Django Template" /> </head> <body> <hi>id : {{id}}</hi> <center>{{context.name}}</center> <center>{{context.say}}</center> <center> <li>{{users}}</li> <li>{{users.0}}</li> </center> {% if context %} <li>test if : user age {{context.age}}</li> {% else %} user not found. {% endif %} {% for user in users %} <li>User: {{user}}</li> {% endfor %} {% for k,v in books.items %} <li>{{forloop.counter}} {{k}}: {{v}}</li> {% endfor %} </body> </html>
正则参数分组时可以不命名分组数据,即 url 可以写成如下形式:
url(r'^hi/index/(d*)/$', 'index'),