User42 User42 - 3 months ago 13
Python Question

including urls of one app into urls file of another app in django

I have two apps in my project, institute and students. I have included the urls of institute app in root url file.

root urls.py

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


institute/urls.py file

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 file

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
]


When I am hitting urls like
localhost/insti/sigin, localhost/insti/<anything>
it works fine, but as soon as I hit the url
localhost/insti/students
it throws me error

'students' is not a registered namespace


somewhere in my code at line
return HttpResponseRedirect(reverse("students:signin"))


Please suggest if the way I am including students urls in institute's url is wrong or there is some other issue?

Answer Source

A better way to procede is :

root urls.py

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

So you can use return HttpResponseRedirect(reverse("students:signin")).

If you dont want to change your urls organisation, use this :

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