1.项目中URL如图
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('app01.urls')), ]
2.APP中的URL如图:
from app01 import views from django.conf.urls import url urlpatterns = [ url(r'^login/$', views.login, name='login'), url(r'^register/$', views.register, name='register'), url(r'^index/$', views.index, name='index'), url(r'^article/(d+)$',views.article,name='article'), url(r'^backend/$', views.backend, name='backend'), ]
3.介绍url
url主要有四个参数
url(regex, view, kwargs=None, name=None) url() 函数具有四个参数: regex(必需)、view(必需)、kwargs(可选)、name(可选)
name用来命名你的URL,这样你就可以在django的其他地方,尤其是模板中,通过名称来明确的引用这个URL,这个强大的特性可以让你,仅仅修改一个文件就可以改变全局的URL模式
4.URL中的namespace
django中可能会有多个app
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^app01/', include('app01.urls', namespace="app01")), url(r'^app02/', include('app02.urls', namespace="app02")), url(r'^admin/', include(admin.site.urls)), ]