在 django 中实现 http404

Posted

技术标签:

【中文标题】在 django 中实现 http404【英文标题】:Implementing http404 in django 【发布时间】:2012-08-24 04:50:41 【问题描述】:

在 django 中找不到实现页面并查看了文档404

我还没有找到页面未找到错误,我在这里做什么

在我的网址代码中,我做了以下操作,

  url(r'^$', 'site_config.views.pagenotfound')

从 django.http 导入 Http404

 def pagenotfound(request):
   return render_to_response('polls/pagenotfound.html', )

【问题讨论】:

有什么问题。你得到一个错误,一个空白页,什么? 对于任何我点击的链接 pagenotfound 函数都会被调用,即使它是一个有效的链接 你的 urls.py 中除了那一行还有其他内容吗? 这个我已经包含在项目的主 urls.py 中,其中已经定义了其他模块 url 【参考方案1】:

你在django中处理404和500的方式是:默认情况下,在templates目录下,创建一个404.html

如果您需要自定义处理程序,只需将这些添加到 urls.py

handler404 = 'views.page_not_found_custom'    
handler500 = 'views.page_error_found_custom'    

按照您想要的方式设计 404.html 页面

【讨论】:

以上是关于在 django 中实现 http404的主要内容,如果未能解决你的问题,请参考以下文章