Django,创建自定义的500/404错误页面 [英] Django, creating a custom 500/404 error page
问题描述
本教程完全符合此处,无法创建自定义的500或404错误页面。如果我输入一个不好的网址,页面给我默认的错误页面。有没有什么我应该检查,这将阻止自定义页面显示?
文件目录:
mysite /
mysite /
__init__.py
__init __。pyc
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
polls /
templates /
admin /
base_site.html
404.html
500.html
polls /
detail.html
index.html
__init__.py
__init __。pyc
admin.py
admin.pyc
models.py
models.pyc
testing.py
urls.py
urls.pyc
view.py
views.pyc
templates /
manage.py
在mysite / settings.py我有这些启用:
DEBUG = False
TEMPLATE_DEBUG = DEBUG
#....
TEMPLATE_DIRS =(
'C :/ Users / Me / Django / mysite / templates',
)
poll / urls.py:
from django.conf.urls import patterns,url
from polls导入视图
urlpatterns = patterns('',
url(r'^ $',views.index,name ='index'),
url(r'^ ?P< poll_id> \d +)/ $',views.detail,name ='detail'),
url(r'^(?P< poll_id> \d +)/ results / $' .results,name ='results'),
url(r'^(?P< poll_id> \d +)/ vote / $',views.vote,name ='vote'),
)
我可以发布任何其他必要的代码,如果我使用不好的网址,我应该如何更改自定义的500错误页面?
修改
解决方案:
我有一个额外的
TEMPLATE_DIRS
在我的settings.py中,导致问题
在您的主要 views.py
下,添加以下两个视图的自定义实现,使用您想要显示的内容设置模板 404.html 和 500.html 。
使用此解决方案,不需要将自定义代码添加到 urls.py
这里是代码:
$ b从django.shortcuts导入render_to_response$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
def handler404 )
response = render_to_response('404.html',{},
context_instance = RequestContext(request))
response.status_code = 404
返回响应
def handler500(request):
response = render_to_response('500.html',{},
context_instance = RequestContext(request))
response.status_code = 500
返回响应
Following the tutorial found here exactly, I cannot create a custom 500 or 404 error page. If I do type in a bad url, the page gives me the default error page. Is there anything I should be checking for that would prevent a custom page from showing up?
File directories:
mysite/
mysite/
__init__.py
__init__.pyc
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
polls/
templates/
admin/
base_site.html
404.html
500.html
polls/
detail.html
index.html
__init__.py
__init__.pyc
admin.py
admin.pyc
models.py
models.pyc
tests.py
urls.py
urls.pyc
view.py
views.pyc
templates/
manage.py
within mysite/settings.py I have these enabled:
DEBUG = False
TEMPLATE_DEBUG = DEBUG
#....
TEMPLATE_DIRS = (
'C:/Users/Me/Django/mysite/templates',
)
within mysite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
I can post any other code necessary, but what should I be changing to get a custom 500 error page if I use a bad url?
Edit
SOLUTION: I had an additional
TEMPLATE_DIRS
within my settings.py and that was causing the problem
Under your main views.py
add your own custom implementation of the following two views, and just set up the templates 404.html and 500.html with what you want to display.
With this solution, no custom code needs to be added to urls.py
Here's the code:
from django.shortcuts import render_to_response
from django.template import RequestContext
def handler404(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
def handler500(request):
response = render_to_response('500.html', {},
context_instance=RequestContext(request))
response.status_code = 500
return response
这篇关于Django,创建自定义的500/404错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!