【问题标题】:Django error templates in custom path自定义路径中的 Django 错误模板
【发布时间】:2017-11-13 07:54:14
【问题描述】:

我有包含所有模板的 dir 模板,并且我有 dir 错误,所有模板都有错误 404.html。 我找到了以下旧解决方案:

在 urls.py 我写的应用程序

from django.conf.urls import (handler400, handler403, handler404, handler500)
handler404 = 'my_app.views.page_not_found'

并在查看我的应用程序

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

def page_not_found(request):
    response = render_to_response(
    'errors/404.html',
    context_instance=RequestContext(request))

    response.status_code = 404
    return response

但我使用的是 Django 1.11,这不起作用。

【问题讨论】:

    标签: python django


    【解决方案1】:

    如果你关注docs

    您应该返回与 HttpResonse 相同的 HttpResponseNotFound,但状态码除外。它对我有用。

    def bad_request(request):
        response = render_to_response(
            'errors/404.html'
        )
    
        return HttpResponseNotFound(response.content)
    

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多