3

I want to display a custom 404 error page when an end user enters a wrong url. I have tried but I am only getting the Django default 404 page. I am using Python 2.7.5 and Django 1.5.4.

My Code:

urls.py

from django.conf.urls import patterns, include, url
from mysite import views
handler404 = views.error404

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

views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.template import Context, loader

def home(request):
     return HttpResponse("welcome to my world")

def error404(request):
     template = loader.get_template('404.html')
     context = Context({'message': 'All: %s' % request,})
     return HttpResponse(content=template.render(context), content_type='text/html; charset=utf-8', status=404)

I have placed my 404.html page in templates directory. How to handle this?

4
  • 1
    Do you have DEBUG mode on? Commented Mar 24, 2014 at 17:00
  • @David Robinson,Ya DEBUG=True Commented Mar 24, 2014 at 17:04
  • possible duplicate? : stackoverflow.com/questions/20102227/… Commented Mar 24, 2014 at 17:06
  • 1
    Definitely not a duplicate of that question. Commented Mar 24, 2014 at 17:17

1 Answer 1

6

Custom URL handlers don't work with DEBUG=True. Set DEBUG=False and it will work. (You'll also need to set ALLOWED_HOSTS=['127.0.0.1'])

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

Comments

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.