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?

有帮助吗?

解决方案

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'])

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top