我的项目中有两个应用程序:学院和学生。我已将学院应用程序的 url 包含在根 url 文件中。
根 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>
这样的网址时它工作正常,但是一旦我点击 url localhost/insti/students
它引发了我的错误
'students' is not a registered namespace
在我的代码中的某处 return HttpResponseRedirect(reverse("students:signin"))
行
我在学院网址中包含学生网址的方式是否错误或者存在其他问题?
最佳答案
更好的方法是:
根 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"))
关于python - 将一个应用程序的 url 包含到 django 中另一个应用程序的 urls 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44658128/