1

I'm having the 404 error when trying to handle a view that is not related to the main page. For example, if I initially start at the main page home, and want to navigate to another page called, otherpage, I receive a 404 otherpage.html not found.

The way I'm doing is based off intuition. So if there's a better way to do this, please mention it:

in the file:

prd/
   views.py
   url.py
   otherstuffthatshouldbehere.py..

I have views.py (this is where I think the error is):

from django.shortcuts import render

def home(request):
    context = {}
    template = "index.html"
    return render(request, template, context)

def otherpage(request):
    context = {}
    template = "otherpage.html"
    return render(request, template, context)

Then urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

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

    url(r'^admin/', include(admin.site.urls)),
)

This returns a 404 for the otherpage.html. I have the template directory working fine. But how do I go about handling multiple views?

EDIT:

Upon adding:

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

I received this error:

Using the URLconf defined in prd.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^about$ [name='about']
^projects$ [name='projects']
^admin/
The current URL, about.html, didn't match any of these.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh that's neat. But now I have the error of Using the URLconf defined in project.urls, Django tried these URL patterns. (I'll add to the main question)
@WillCampbell I made an edit that hopefully explains the cause of that issue.
Awesome, that helped immensely. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.