在django工程比较小的时候,所有的路由都可以写在一个urls.py中。但当app逐渐增多的时候,都写在一个文件中会显得越来越臃肿。这时必然会涉及urls分级,即在总的urls.py中引入各个app的urls.py文件。
假设目前工程中有两个app,名称分别为app1,app2,在每个app中分别有一个urls.py.
在总的urls.py中引入它们的方法为:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^app1/', include('app1.urls')),
url(r'^app2/', include('app2.urls')),
]
app1的urls.py文件内容为:
#coding=utf8
from django.conf.urls import url, include
from django.contrib import admin
from app1 import views as app1_view
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^app1_test/', app1_view.app1_test,name='app1_test'),
]
工程运行起来之后,访问路径为:http://localhost:8000/app1/app1_test/
网友评论