我正在尝试为HTTP 404
错误创建自定义 404.html 页面。
对于此 url 127.0.0.1:8000/polls/Books/dsfdsf/
,它显示Page not found (404)
默认调试页面,而DEBUG=True
. 对于相同的 url,我想创建自定义 404 页面。
我已经为此浏览了Django Docs。
我已采取以下步骤:
1)。DEBUG=False
在settings.py
2)。handler404 = 'pollsite.views.custom_404'
在urls.py
3)。视图.py
def custom_404(request):
return render('404.html', context_instance=RequestContext(request))
4)。我已将404.html
文件放在模板目录中,TEMPLATE_DIRS
404.html
现在我希望当我尝试访问 127.0.0.1:8000/polls/Books/dsfdsf 时会出现自定义错误页面,对吗?相反,我得到了TemplateDoesNotExist: 500.html
甚至显示的有效 url TemplateDoesNotExist: 500.html
为什么它不生成 HTTP 404 错误,从而调用自定义404.html
文件?
我尝试了question1和question2提到的解决方案,
但仍然出现烦人的错误TemplateDoesNotExist: 500.html
请指出是什么问题?
编辑:错误堆栈
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 153, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
return callback(request, **param_dict)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/defaults.py", line 32, in server_error
t = loader.get_template(template_name) # You need to create a 500.html template.
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 145, in get_template
template, origin = find_template(template_name)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html
.........................解决方案:
1)在settings.py中设置ALLOWED_HOSTS = ['127.0.0.1']
2)在views.py中 return render_to_response('404.html')