django中自定義404錯誤頁面

自定義404頁面,以下5個步驟:
1)使用自定義的404頁面,必須在setting文件修改DEBUG = False(即關閉debug調試模式)
2)必須在setting文件修改ALLOWED_HOSTS = ['*']或者ALLOWED_HOSTS = ['127.0.0.1', 'localhost''],不然啓動項目會提示:
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
3)在urls文件中,添加以下代碼:
from django.conf.urls import handler404, handler500
handler404 = sign_views.page_not_found #handler404 = "你的app.views.函數名"
4)在views文件中添加page_not_found函數
def page_not_found(request):
return render_to_response("404.html")
5)在app的templates下創建404.html
相關文章
相關標籤/搜索