Reputation: 7028
I am following this guide here tango with django and I am reading/copying as best from here tutorial part 3
But I cannot get different views to display different content. I am missing the explanation somewhere/somehow.
so this is my project urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Sayth.views.home', name='home'),
# url(r'^Sayth/', include('Sayth.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')),
url(r'^rango/about/', include('rango.urls')),
)
This is my application urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.about, name='about'),
)
These are my views
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says Hello World! <a href='/rango/about'>About</a>" )
def about(request):
return HttpResponse("This is the about page. <a href='/rango/'>Index</a>")
Both my pages resolve but both show the index page, why?
Upvotes: 0
Views: 412
Reputation: 253
As the first answer have mentioned, your project's urls.py only needs one pointer to the application urls.py.
Conceptually it is like this, anything matching '^rango/' should be passed onto the application urls.py, and then the applications handles the rest of the url.. either '/' or 'about/'
So in your applications urls.py, you then need to ensure that a mapping for each of these patterns exists.
In the tangowithdjango book, the about page is an exercise, left for the reader, but if you have future problems you can always inspect the code for the application, for example:
The project urls.py:
The application urls.py:
https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/rango/urls.py
Upvotes: 1
Reputation:
If you do not wish to create an urls.py on the app you can just do the url mapping like this:
urlpatterns = patterns('',
# ...
# most detailed urls comes first.
url(r'^rango/about/?$', 'rango.views.about', name='about'),
url(r'^rango/?$', 'rango.views.index', name='index'),
)
Upvotes: 0
Reputation: 2382
The urls defined are not correct. The root urls.py should have only a pointer to the app, and the app's urls.py should resolve the different endpoints.
Try the following urls.py:
#Project urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Sayth.views.home', name='home'),
# url(r'^Sayth/', include('Sayth.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')),
)
#rango/urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
)
Upvotes: 1