Py学习  »  问与答

[求助]django 自定义404 错误

danpy5 • 12 年前 • 13542 次点击  

错误信息为:A server error occurred. Please contact the administrator.

urls.py

from django.conf.urls.defaults import *
from django.views.generic import list_detail
from blog.views import index, about
from blog.models import User

handler404 = 'blog.views.page_not_found'
handler500 = 'blog.views.page_error'

urlpatterns = patterns('',

url(r'^admin/', include(admin.site.urls)),
url(r'^$', index),
url(r'^hello/(\w+)/$',about),
url(r'^user/$',list_detail.object_list, user_info),
)

=========================================================

views.py

from django.shortcuts import render_to_response

def page_not_found(request):
    return render_to_response('404.html')

def page_error(request):
    return render_to_response('500.html')

==================================================

在app的 templates 目录中 添加了 404和500的html 模板 。 500的 模板是能出来效果 但是所有的页面都只显示500的页面 。 这到底是那里出错了啊,新手求助啊~~~~

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/92
 
13542 次点击  
文章 [ 6 ]  |  最新文章 11 年前
rcompass
Reply   •   1 楼
rcompass    11 年前

在settings.py加入以下语句即可,ALLOWED_HOSTS = ['localhost', '192.168.1.101', '192.168.1.102', ''],生产环境中不要用'' 具体参考http://stackoverflow.com/questions/15128135/django-setting-debug-false-causes-500-error

Py站长
Reply   •   2 楼
Py站长    12 年前

@danpy123 我估计 是你的 settings.py中的 DEBUG 设置成 TRUE了吧?

danpy5
Reply   •   3 楼
danpy5    12 年前

我把404.html 和 500.html文件放到 templates 根目录里以后, 网页只显示500里的内容. 就算在url里定义的page也 不能进.

Py站长
Reply   •   4 楼
Py站长    12 年前

只要将400.html 500.html 放在template根目录下就OK了, 不需要这么复杂啊

danpy5
Reply   •   5 楼
danpy5    12 年前

哦, 刚加没多久还不太熟悉不好意思。

Py站长
Reply   •   6 楼
Py站长    12 年前

http://django-china.cn/forum/techqa/ 在这里提问哦~