如何为Django应用中不存在的路径返回404?

时间:2019-07-03 19:07:48

标签: python django

我注意到当我击中某些无效的URL时收到HTTP 204“无内容”。例如,如果我错误地键入myapp.com/loggin而不是myapp.com/login,我期望使用HTTP404。如何使 any 无效网址在Django中返回404?< / p>

1 个答案:

答案 0 :(得分:0)

views.py

def handler_error_404(request, exception):
    return render(request, '404.html', status=404)

404.html是您的自定义404页面

在您的主要urls.py

from your_app.views import handler_error_404

handler404 = handler_error_404

# Here put your urlpatterns

确保,在settings.py中,DEBUG设置为False