当我使用runserver时,它会发出以下警告消息:
(1_8.W001)Django 1.8中不推荐使用独立的TEMPLATE_*设置,TEMPLATES字典优先.您必须将以下设置的值放入默认的TEMPLATES dict:TEMPLATE_DEBUG.
Django文档:
"TEMPLATE_DEBUG从版本1.8开始不推荐使用:在DjangoTemplates后端的OPTIONS中设置'debug'选项."
这是我的settings.py与我徒劳的尝试修复它:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'myapp/templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'debug': DEBUG,
'DEBUG': DEBUG,
'TEMPLATE_DEBUG': DEBUG
},
}, ]
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
我有一个我在django工作的项目.我有很多例子:
raise Http404("this is an error")
Run Code Online (Sandbox Code Playgroud)
它为我创建了一个很好的404页面,上面写着错误消息"这是一个错误".
我现在想要创建一个自定义错误页面并让它仍然显示消息,但我无法弄清楚如何.
我确定它只是一个模板变量,我需要添加到我的自定义404模板,但我找不到任何文档.
如果urls.py中不存在任何类型的url模式到创建的页面"404.html"页面,而不是由django显示错误,如何将其重定向到.
我正在实现自定义404和500模板,但是404.html模板似乎返回request.user.is_authenticated很好,但是500.html模板无法返回任何内容。我还检查了request.user,它在500页上只是空白。
这很奇怪,因为当我触发500错误时,我收到了预期的错误报告电子邮件,并且显然在请求明细中正确定义了USER。这是我在views.py中使用的代码:
def handler404(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
def handler500(request):
response = render_to_response('500.html', {},
context_instance=RequestContext(request))
response.status_code = 500
return response
Run Code Online (Sandbox Code Playgroud)
我想知道后台(也许在RequestContext中)是否在处理500与404不同的问题?我应该提到,我也在使用django-guardian,尽管我认为这不会影响任何情况。有任何想法吗?
编辑: 此评论声称“ 500模板将不会呈现request.user,因为它报告了500服务器错误,因此服务器无法提供任何服务。” 有谁知道解决这个问题的方法吗?似乎应该有一个,因为,就像我说的那样,我在错误报告电子邮件中收到的日志显然带有带有用户名的请求对象。
编辑2:现在我想知道它是否与django-allauth有关-我也在使用它。
django django-templates django-errors django-allauth django-1.6
没有让事情变得困难,我只想展示一个带有静态文件的特殊404渲染.
如果设置DEBUG = False,则可以在urls.py中使用
handler404 = 'app.views.handler404'
Run Code Online (Sandbox Code Playgroud)
但它没有静态文件.我不想为一个简单的应用程序安装Web服务器.
随着DEBUG = True在网址
url(r'^404/$', views.handler400)
Run Code Online (Sandbox Code Playgroud)
未覆盖默认页面未找到(404)页面.
实现渲染的简单方法是什么,例如当DEBUG = True时用staticfiles 键入localhost/asdfhjfsda?
提前致谢...
我试图为 404 页创建一个自定义处理程序,但出现此错误。我已经尝试了很多方法,但我的观点似乎不起作用?
handler404 = 'DEMOPROJECT.views.handler404'
handler500 = 'DEMOPROJECT.views.handler500'
Run Code Online (Sandbox Code Playgroud)
def handler404(request, exception, template_name="404.html"):
response = render_to_response('/not-found')
response.status_code = 404
return render(request, response)
def handler500(request, exception, template_name="500.html"):
response = render_to_response('/not-found')
response.status_code = 500
return render(request, response)
Run Code Online (Sandbox Code Playgroud)
ERRORS:
?: (urls.E007) The custom handler500 view 'DEMOPROJECT.views.handler500' does not take the correct number of arguments (request).
System check identified 1 issue (0 silenced).
Run Code Online (Sandbox Code Playgroud) django ×6
python ×4
django-1.6 ×1
django-1.8 ×1
django-urls ×1
django-views ×1
regex ×1
templates ×1