How to display custom 404.html page in Django

做~自己de王妃 提交于 2019-12-04 15:48:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!