簡體   English   中英

Django3,自定義 404 頁面在 Debug=False 時重定向到 500,因為 Django 找不到異常參數

[英]Django3, custom 404 page redirects to 500 when Debug=False because Django cannot find exception parameter

Debug = True django 返回一個Page not found (404)頁面,這完全沒問題

但是當我更改Debug = False django 返回Server Error (500)並且服務器顯示這些請求時:

[30/Jul/2022 12:29:44] "GET /__reload__/events/ HTTP/1.1" 500 145
[30/Jul/2022 12:29:44] "GET /__reload__/events/ HTTP/1.1" 500 145
[30/Jul/2022 12:29:44] "GET /sdfs HTTP/1.1" 500 145
[30/Jul/2022 12:29:45] "GET /favicon.ico HTTP/1.1" 302 0

我的項目主urls.py

from django.conf.urls import handler404
handler404 = 'myproject.views.custom_page_not_found'

請注意此處未找到hander404 django handler404 not found

我的views.py

def custom_page_not_found(request, exception, template_name="errors/404.html"):
    return render(request, template_name, status=404)

請不要在這里 django 找不到exception參數exception parameter not found

在以前的社區問題中,我找不到任何指向exception參數未找到問題的問題。 請注意,我沒有在我的404.html文件中使用 django 模板{% %}

我認為當 django 沒有找到異常參數時,它會返回服務器錯誤 500。

您不需要from django.conf.urls import handler404導入此處理程序。

您應該簡單地在 url 中定義變量:

handler404 = 'myproject.views.custom_page_not_found'

作為 function 的視圖(更好),您可以這樣做:

def custom_page_not_found(request, *args, template_name="errors/404.html", **kargs):
    return render(request, template_name, status=404)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM