问题
I want to display custom 404 error page when end user enters wrong url,I have tried but i am getting only Django default 404 page.I am using Python(2.7.5),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..?
回答1:
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']
)
来源:https://stackoverflow.com/questions/22615572/how-to-display-custom-404-html-page-in-django