Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自定义路径中的Django错误模板_Python_Django - Fatal编程技术网

Python 自定义路径中的Django错误模板

Python 自定义路径中的Django错误模板,python,django,Python,Django,我有dir模板和所有模板,我有dir错误,所有模板都有错误404.html。 我找到了以下旧的解决方案: 在URL.py中,我编写了我的应用程序 from django.conf.urls import (handler400, handler403, handler404, handler500) handler404 = 'my_app.views.page_not_found' 在我的应用程序中查看 from django.shortcuts import (render_to_resp

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

在URL.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,这不起作用。

如果您按照

您应该返回HttpResponseNotFound,它与HttpResponse相同,只是状态代码不同。这对我有用

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

    return HttpResponseNotFound(response.content)
如果你遵循

您应该返回HttpResponseNotFound,它与HttpResponse相同,只是状态代码不同。这对我有用

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

    return HttpResponseNotFound(response.content)