路由系统 project: django-admin startproject mysite cd mysite python3 manage.py startapp app01 mysite mysite - urls.py #路由系统 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^login/(\d+)/(\d+)/', views.login), # 后面正则表达式匹配的内容作为参数传入函数中,多个参数按顺序传入 url(r'^page/(?P<p1>\d+)/(?P<x2>\d+)/', views.page), # ?P<xxx>:为传入的参数取名xxx,为参数取名后,传入参数时函数的参数名就为这里定义的。函数中的参数可以不按顺序传。 普通关系:url(r'^index/', views.index), /index/ -> 函数 /login/ -> 函数 ... # 反射:------可以基于此做自动路由:http://www.cnblogs.com/wupeiqi/articles/5237704.html /(\w+)/(\w+) /home/index => 导入:home模块,getattr(index) fun() 动态关系: url(r'^login/(\d+)/(\d+)/', views.index), /login/(\d+)/(\d+)/ -> index(request, n, x) #参数可以任意取名,参数传入是按顺序的 url(r'^page/(?P<p1>\d+)/(?P<x2>\d+)/', views.page), /page/(?P<p1>\d+)/(?P<x2>\d+)/ -> page(request, p1, x2) # 按照url中的名字来传参,函数中的参数名必须和url中定义的名一样。可以不按顺序来传参 路由分发: mysite mysite - urls.py from django.conf.urls import url, include from django.contrib import admin # 路由系统 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^web/', include('app01.urls')), #所有访问/web/的url都转到app01.urls.py来处理 ] app01 - urls.py # 自己创建,用来处理mysite.urls.py转过来的url from django.conf.urls import url from app01 import views # 路由系统 urlpatterns = [ url(r'^index/(\d+)/', views.index), url(r'^detail/(\d+)/', views.detail), ]
关注我的公众号,不定期推送资讯
本文来自博客园,作者:链条君,转载请注明原文链接:https://www.cnblogs.com/MacoLee/articles/6253013.html