User42 User42 - 7 days ago 4
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.


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

institute/ 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/ 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
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?


A better way to procede is :


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"))