我按照文檔的步驟逐步搜索,并在任何地方搜索解決方案,但我的自定義不起作用。我將詹戈2.2與Python 3.8一起使用。handler500這是我 urls.py:urlpatterns = [ # some urls]handler500 = "path.to.my.handler"我的處理程序:def handler500(request, exception=None, *_, **_k): print("yeah I got called") return render(request, "my_template.html", { "exception": exception })我的觀點(diǎn):def example_view(request): # I tried all of these return HttpResponseServerError() return HttpResponse(status=500) raise Exception("There was an error") # This just shows: "A server error occurred. Please contact the administrator." in the browser. raise HttpResponseServerError() # This shows the same message as above. a_typo # This typo also shows the same message as above.為什么這些錯(cuò)誤中的任何一個(gè)都不顯示我的模板?處理程序在任何時(shí)候都不會(huì)被執(zhí)行。該函數(shù)從未被調(diào)用。print()編輯我設(shè)置了一個(gè)404處理程序并對(duì)其進(jìn)行了測(cè)試,它工作得很好。為什么不是500?
3 回答

DIEA
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊
找到解決方案
我有一個(gè)名為 的設(shè)置設(shè)置為 。這似乎禁用了我的自定義處理程序。它現(xiàn)在完美地工作。DEBUG_PROPAGATE_EXCEPTIONS
True

HUH函數(shù)
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

慕絲7291255
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
引用詹戈文檔
如果實(shí)現(xiàn)自定義視圖,請(qǐng)確保它接受參數(shù)并返回 .您必須在 .
request
HttpResponseServerError
HttpResponseServerError
handler500
實(shí)現(xiàn)所有這些的一個(gè)非常簡(jiǎn)單的方法就是包含在您的視圖中
def handler500(request, exception, template_name="my_template.html"): response = render_to_response("my_template.html") response.status_code = 500 return response
這樣,您就不需要更改 URL 協(xié)議中的任何內(nèi)容。此方法確實(shí)要求模板位于根模板文件夾的頂部。也許把它命名為“500.hml”,它會(huì)跳到頂部。
添加回答
舉報(bào)
0/150
提交
取消