django教程:500 @ debug = false

See*_*ull 6 django debugging python-2.7

Windows 7
Python 2.7.3
Django 1.5
python manage.py runserver

我正在关注" https://docs.djangoproject.com/en/1.5/intro/tutorial03/ "上提供的教程.

在settings.py中使用DEBUG = True可以正确生成网页.地址

http://127.0.0.1:8000/admin/  
Run Code Online (Sandbox Code Playgroud)

显示管理员登录页面.

http://127.0.0.1:8000/polls/  
Run Code Online (Sandbox Code Playgroud)

显示'什么事?' 项目符号链接.

http://127.0.0.1:8000/polls/1/  
Run Code Online (Sandbox Code Playgroud)

显示数字'1'

http://127.0.0.1:8000/polls/2/  
Run Code Online (Sandbox Code Playgroud)

显示标准404消息.但是,如果我设置DEBUG = False(并且不执行任何其他操作),那么我在所有地址都会出现500错误!我正在使用开发内部运行服务器.没有回溯错误.服务器日志如下所示:

DEBUG = True

0 errors found  
March 19, 2013 - 12:01:12  
Django version 1.5, using settings 'mysite.settings'  
Development server is running at http://127.0.0.1:8000/  
Quit the server with CTRL-BREAK.  
[19/Mar/2013 12:06:41] "GET /admin/ HTTP/1.1" 200 1893  
[19/Mar/2013 12:54:55] "GET /polls/ HTTP/1.1" 200 74  
[19/Mar/2013 12:55:02] "GET /polls/2/ HTTP/1.1" 404 1577  
[19/Mar/2013 12:55:09] "GET /polls/1/ HTTP/1.1" 200 1  
Validating models...  
Run Code Online (Sandbox Code Playgroud)

DEBUG = False

0 errors found  
March 19, 2013 - 12:55:21  
Django version 1.5, using settings 'mysite.settings'  
Development server is running at http://127.0.0.1:8000/  
Quit the server with CTRL-BREAK. 
[19/Mar/2013 12:59:22] "GET /admin/ HTTP/1.1" 500 27  
[19/Mar/2013 12:59:25] "GET /polls/ HTTP/1.1" 500 27  
[19/Mar/2013 12:59:28] "GET /polls/1/ HTTP/1.1" 500 27  
[19/Mar/2013 12:59:34] "GET /polls/2/ HTTP/1.1" 500 27  
Run Code Online (Sandbox Code Playgroud)

我的目录结构如下:

1> mysite  
2>     - mysite  
3>     - polls  
4>         - templates  
5>             - polls  
6>     - templates  
7>         - admin  
Run Code Online (Sandbox Code Playgroud)

我的'manage.py'是@ 1>所以我知道我必须在以下文件和位置进行编辑.

@ 2> settings.py
@ 2> urls.py

但是:
答:我目前没有'view.py'文件@ 2>,这是一个新文件吗?按照教程后我现在有一个'view.py'@ 3>;
B.'404.html'是否应该进入@ 2>下的新模板目录或两个现有模板目录中的一个(@ 4>或@ 6>)?我假设自定义'500.html'将进入同一目录.

我很乐意发布您想要查看的更多代码.

Zde*_*vic 10

很可能,您忘记ALLOWED_HOSTS设置文件设置.

  • 遗憾的是,在settings.py中设置'ALLOWED_HOSTS = ['localhost','127.0.0.1']'无效.我也试过'ALLOWED_HOSTS ='*'没有任何好处. (9认同)

hel*_*013 5

之前我有同样的问题,我能够通过在我的模板目录中创建404.html来修复此问题,指定有效的ALLOWED_HOSTS并重新启动我的网络服务器


epy*_*nkn 5

我遇到了类似的问题,并且能够用最少量的代码修复它,假设您的 django 项目名为 mysite:

# mysite/mysite/settings.py
DEBUG = False
ALLOWED_HOSTS = ['localhost']
# Tells your project to find your custom 404.html at mysite/mysite/templates/404.html
INSTALLED_APPS = (..., 'tutorial', ...)

# mysite/mysite/urls.py
handler404 = 'tutorial.views.custom_404'

# mysite/mysite/views.py
from django.shortcuts import render

def custom_404(request):
    return render(request, '404.html', {}, status=404)

# mysite/mysite/templates/404.html
<h1>CUSTOM 404 PAGE</h1>
Run Code Online (Sandbox Code Playgroud)

不要忘记重新启动服务器。=P


目录结构应如下所示:

mysite  
    - mysite
        - settings.py
        - urls.py
        - views.py
        - templates
            - 404.html  
    - polls  
        - templates  
            - polls
Run Code Online (Sandbox Code Playgroud)