路由分发相关

主路由

from app01 import urls as app01_urls
from app01 import urls as app02 urls
url(r'^app01/',include(app01_urls))
url(r'^app02/',include(app02_urls))


子路由app01\urls.py

from dhango.conf.urls import url
from app01 import views

urlpatterns = [
url(r'^index/',views.index)
]

如果在两个应用下同时反向解析,那么解析的内容无法正确定位,因此我们在命名函数的时候,在views.py的函数名前面加上app01_funcname最好。

名称空间namespace 可以替代以上的做法,但是并不推荐:

from app01 import urls as app01_urls
from app01 import urls as app02 urls
url(r'^app01/',include(app01_urls,namespace='app01'))
url(r'^app02/',include(app02_urls,namespace='app02'))

我们在app01的views.py中进行反向解析时:

def index(request):
	print(reverse(‘app01:aaa’)) 

带上app01: 即可

posted @ 2019-04-16 15:17  不会玩python  阅读(3)  评论(0编辑  收藏  举报