0

我正在使用 django 1.1.4 和 python 2.6.6 和 mod_wsgi 来建立一个网站

客户希望 404 和 500 页面有他的标志和主页链接。我写了简单的页面。它大于 512 字节。我已经调整了 PROJECT_ROOT/templates/ 中的文件

如果我将调试设置为True错误,则表示其为 404 错误。但是当我将它设置为False并尝试加载一个不存在的页面时,我得到一个内部服务器错误,如下所示:

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请联系服务器管理员 root@localhost 并告知他们错误发生的时间,以及您所做的任何可能导致错误的事情。

服务器错误日志中可能提供有关此错误的更多信息。Apache/2.2.15 (CentOS) 服务器位于 www.anglais-verbe.com 端口 80

我应该怎么办?

我没有编写任何处理 404、500 的视图或对 urls.py 进行更改。

这是我第一个成熟的 django 项目,我不确定出了什么问题。

更新:

我的管理员回溯电子邮件是

File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 125, in get_response
   callback, param_dict = resolver.resolve404()

 File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 267, in resolve404
   return self._resolve_special('404')

 File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 259, in _resolve_special
   callback = getattr(self.urlconf_module, 'handler%s' % view_type)

AttributeError: 'module' object has no attribute 'handler404'
4

2 回答 2

2

您可以在主 urls.py 中设置 404 处理程序:

handler404 = 'yourproject.views.file_not_found_404'

那么你必须在你的views.py中创建以下视图:

from django.shortcuts import render_to_response
from django.template import RequestContext

def file_not_found_404(request):
    #create some variables here if you like
    path = request.path
    response = render_to_response('404.html', locals(),
                              context_instance=RequestContext(request))
    response.status_code = 404
return (response)

然后您可以在模板文件夹中为您的 404 创建一个模板:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL {{ path }} was not found on this server.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at yourdomain.com port 80</address>
</body></html>

希望这会有所帮助:) 这也应该适用于 500 错误

于 2012-01-09T17:25:25.850 回答
1
  1. 仔细检查您的模板文件是否已命名404.html并且500.html
  2. 如果debug=FALSE是,则默认 Django 行为是向管理员发送电子邮件。回溯说什么?
  3. 如果您没有这封邮件,请查看 apache 错误日志,看看是否有任何线索。在 Debian 上(我不知道 CentOS),假设 apache2,位置是

    tail /var/log/apache2/error.log
    
于 2012-01-09T17:27:14.773 回答