django教程:500@debug=false
Windows 7django教程:500@debug=false,django,python-2.7,debugging,Django,Python 2.7,Debugging,Windows 7 Python 2.7.3 Django 1.5 python manage.py运行服务器 我正在关注“网站”上提供的教程 如果settings.py中的DEBUG=True,则会正确生成网页。地址 http://127.0.0.1:8000/admin/ 显示管理员登录页面 http://127.0.0.1:8000/polls/ 显示带项目符号的“最近怎么样”链接 http://127.0.0.1:8000/polls/1/ 显示数字“1” http:/
Python 2.7.3
Django 1.5
python manage.py运行服务器 我正在关注“网站”上提供的教程 如果settings.py中的DEBUG=True,则会正确生成网页。地址
http://127.0.0.1:8000/admin/
显示管理员登录页面
http://127.0.0.1:8000/polls/
显示带项目符号的“最近怎么样”链接
http://127.0.0.1:8000/polls/1/
显示数字“1”
http://127.0.0.1:8000/polls/2/
显示标准404消息。然而,如果我设置DEBUG=False(并且不做其他事情),那么我在所有地址都会得到500个错误!我正在使用开发内部runserver。没有回溯错误。服务器日志如下所示:
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...
使用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
我的目录结构如下:
1> mysite
2> - mysite
3> - polls
4> - templates
5> - polls
6> - templates
7> - admin
我的“manage.py”是@1>,因此我知道我必须在以下文件和位置中进行编辑
@2>设置。py@2>URL.py 然而:
答:我目前没有'view.py'文件@2>,这是新文件吗?学习完教程后,我现在有了一个“view.py”@3>
B.“404.html”应该进入@2>下的新模板目录还是两个现有模板目录(@4>或@6>)中的一个?我假设一个自定义的“500.html”会进入同一个目录
我很乐意发布更多您想要查看的代码。很可能是您忘了在中设置
允许的\u主机。我以前也遇到过同样的问题,我可以通过在模板目录中创建404.html来解决这个问题,指定有效的允许的\u主机并重新启动我的Web服务器我遇到了类似的问题,并且能够用最少的代码修复它,假设您的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>
试着像这样运行服务器
python manage.py运行服务器本地主机:8000
如果你想让它在所有地址工作
python manage.py runserver 0.0.0.0:8000以下是在我的案例中起作用的内容:
- 我刚刚在模板中添加了404.html
- 我还做了
python manage.py collectstatic--noinput
它为我解决了这个错误。很遗憾,我在开发服务器上的本地出现了问题,可能是重复的。我离生产环境很远。遗憾的是,在settings.py中设置'ALLOWED_HOSTS=['localhost','127.0.0.1']'无效。我还尝试了'ALLOWED_HOSTS='*',但没有任何好处。AFAIK缺少ALLOWED_HOSTS
将导致400(错误请求)错误,而不是500(内部服务器错误)。这不应该是问题的原因,至少不是问题的根本原因。我做了与SeeGull相同的事情,但仍然收到错误。我不需要重新启动Web服务器。您是否将handler404='tutorial.views.custom_404'放在urlpatterns的内部或外部?你的答案中的“教程”是什么?我在你的目录结构中没有看到。
mysite
- mysite
- settings.py
- urls.py
- views.py
- templates
- 404.html
- polls
- templates
- polls