包括一个应用程序的URL到django中另一个应用程序的urls文件中

时间:2017-06-20 16:12:43

标签: python django url

我的项目,学院和学生都有两个应用程序。我在根网址文件中包含了该机构app的网址。

root urls.py

urlpatterns += [    
    url(r'^insti/', include('institute.urls', namespace='institute')), 
]

institute / urls.py文件

from institute import views
from django.conf.urls import url, include

app_name = "institute"
# institute urls
urlpatterns = [
    url(r'^$', views.institute_home, name='home'),
    url(r'^signin/$', views.signin, name='signin'),
    url(r'^logout/$', views.institute_logout, name='logout'),
    # other urls here
]
# students urls in institute
urlpatterns += [    
    url(r'^student/', include('students.urls', namespace='students')),
]

students / urls.py文件

from django.conf.urls import url
from students import views

app_name = "students"
urlpatterns = [
    url(r'^$', views.students_home, name="students_home"),
    url(r'^register/$', views.register, name="register"),
    url(r'^signin/$', views.signin, name="signin"),
    url(r'^logout/$', views.students_logout, name="logout"),
    # other urls
]

当我点击localhost/insti/sigin, localhost/insti/<anything>这样的网址时,它可以正常工作,但是只要我点击网址localhost/insti/students就会引发错误

  

&#39;学生&#39;不是注册名称空间

我的代码中某处return HttpResponseRedirect(reverse("students:signin"))

我在学院网址中包含学生网址的方式是错误还是存在其他问题?

1 个答案:

答案 0 :(得分:1)

更好的方法是:

root urls.py

urlpatterns += [
    url(r'^insti/student/', include('students.urls', namespace='students')),
    url(r'^insti/', include('institute.urls', namespace='institute')),
]

所以你可以使用return HttpResponseRedirect(reverse("students:signin"))

如果您不想更改您的网址组织,请使用以下网址:

return HttpResponseRedirect(reverse("institue:students:signin"))