五、Django之路由系统
1、普通路由匹配
URL配置(URLconf)就像Django 所支撑网站的目录。它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表;它就是以这种方式告诉Django,用哪个URL调用哪段代码。URL的加载是从配置文件中开始。
urlpatterns = [
url(正则表达式, views视图函数,参数,别名),
]
参数说明:
- 一个正则表达式字符串
- 一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串
- 可选的要传递给视图函数的默认参数(字典形式)
- 一个可选的name参数
例子:
from django.conf.urls import url
from . import views
urlpatterns = [
#匹配articles开头,2003结尾的url,精确匹配。
url(r'^articles/2003/$', views.special_case_2003),
#articles开头,4个数字结尾的url,模糊匹配
url(r'^articles/([0-9]{4})/$', views.year_archive),
#articles开头,再接4位数字,最后接2位数字结尾的url
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
#articles开头,再接4位数字,再接2位数字,最后接数字,至少1个数字
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]
2、命名路由匹配
通过正则表达式进行匹配url。
from django.conf.urls import url
from . import views
urlpatterns = [
#匹配articles开头,2003结尾的url
url(r'^articles/2003/$', views.special_case_2003),
#匹配articles开头,视图函数包含4位数字的year参数
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
#匹配articles开头,视图函数包含4位数字的year参数,2位数字的month参数
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
#匹配articles开头,包含4位数字的year,2位数字的month,2位数字的day
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
]
3、二级路由(Including)
如果映射 url 太多怎么办,全写一个在 urlpatterns 显得繁琐,所以二级路由应用而生。所以include主要是用于减少重复代码。
此外,还有一个应用场景,就是在多个app里面,使用include将url分开,这样就不会显得url有很多,且看上去简洁明了。
例子1:
作用:简单明了。
from django.conf.urls import include, url
from apps.main import views as main_views
from credit import views as credit_views
extra_patterns = [
url(r'^reports/$', credit_views.report),
url(r'^reports/(?P<id>[0-9]+)/$', credit_views.report),
url(r'^charge/$', credit_views.charge),
]
urlpatterns = [
url(r'^$', main_views.homepage),
url(r'^help/', include('apps.help.urls')),
url(r'^credit/', include(extra_patterns)),
]
例子2:
作用:减少重复代码。
# include前版本
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/history/$', views.history),
url(r'^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/edit/$', views.edit),
url(r'^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/discuss/$', views.discuss),
url(r'^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/permissions/$', views.permissions),
]
# include后版本
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/', include([
url(r'^history/$', views.history),
url(r'^edit/$', views.edit),
url(r'^discuss/$', views.discuss),
url(r'^permissions/$', views.permissions),
])),
]
例子3:多app分散url
作用:url分配到对应的app,简洁明了。
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls')),#一个为blog的app的url单独为一个文件
]
4、传递额外的参数到视图函数
作用:对于某一批URL具有固定参数的时候就可以使用此方法。
urlpatterns = [
url(r'^blog/(?P<year>[0-9]{4})/$', views.year_archive, {'foo': 'bar'}),
]
#视图函数里面调用
views.year_archive(request, year='2005',foo='bar')
5、带别名的url
作用:对于url很长的时候比较有用。
urlpatterns = [
url(r'^index',views.index,name='myindex'),
]
#调用别名
<body>
{#<form action="/index/" method="post">#}
<form action="{% url 'myindex' %}" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="submit">
</form>
</body>