我正在尝试在我的django网站上开发自定义404错误页面。我按照link实现了所有内容。但是当我用无效的网址尝试它时出现问题。
TemplateDoesNotExist: 500-page.html
handler500 = 'polls.views.error500'
而不是mysite.views.error500
为什么它无法识别我的模板。
根据链接,我将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 "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 156, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 224, in handle_uncaught_exception
return callback(request, **param_dict)
File "/home/anuj/Envs/estoppeleasy/letterproject/estoppeleasy/web/views.py", line 68, in error500
return render(request,'500-page.html')
File "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 53, in render
return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/template/loader.py", line 170, in render_to_string
t = get_template(template_name)
File "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/template/loader.py", line 146, in get_template
template, origin = find_template(template_name)
File "/home/anuj/Envs/estoppeleasy/local/lib/python2.7/site-packages/django/template/loader.py", line 139, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500-page.html
答案 0 :(得分:3)
您的模板应命名为500.html
,404.html
不是500-page.html
。您不需要任何额外的视图。这一切都是开箱即用的。删除您可能已添加到urls.py
,settings.py
或views.py
的有关500错误的所有额外内容。
只需将500.html
文件放入您的templates-root文件夹即可。
请注意不要延长base.html
。创建一个全新的干净的自运行html文件,不包含任何包含或扩展。
答案 1 :(得分:0)
创建名为500.html
和404.html
的模板并放在root folder of template directory
中,它会起作用。