【问题标题】:How to display custom 404.html page in Django如何在 Django 中显示自定义 404.html 页面
【发布时间】:2014-05-02 04:10:25
【问题描述】:

我想在最终用户输入错误网址时显示自定义 404 错误页面。我已经尝试过,但我只得到了 Django 默认的 404 页面。我正在使用 Python 2.7.5 和 Django 1.5.4。

我的代码:

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)

我已将我的404.html 页面放在模板目录中。如何处理?

【问题讨论】:

标签: python django django-views django-urls


【解决方案1】:

自定义 URL 处理程序不适用于 DEBUG=True。设置DEBUG=False 就可以了。 (您还需要设置ALLOWED_HOSTS=['127.0.0.1']

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2018-05-28
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2010-11-24
    • 2018-08-05
    • 2013-03-02
    相关资源
    最近更新 更多