[Answered ]-Handling Multiple Views/ Multiple Urls in Django

2👍

The urlpatterns starts at the top and then goes down until it matches a URL based on the regex. In order for Django to serve up the page located at otherpage.html there has to be a URL defined in urlpatterns that matches otherpage.html otherwise you will get the 404.

In this case you have:

url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),

Note that nothing will ever get to otherpage here because home has the same pattern (regex) and will therefore always match first. You want to have a different URL for both those so that you can differentiate between them. Perhaps you could do something like:

url(r'^$', 'prd.views.home', name='home'),
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),

After making this change you now have a match for otherpage and hopefully no more 404.

EDIT:

url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),

This matches www.example.com/otherpage.html but it will not match www.example.com/otherpage

To match www.example.com/otherpage you need to use:

url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),

Note the difference in the regex, there’s no .html here. The regex matches exactly what you put in it.

Leave a comment