Reprinted Django 500,404,400 bug fixes Optimization

Reprinted: https://blog.csdn.net/qq_38038143/article/details/80105653

  • Error 404: page not found View
  • 500 Error: server error view
  • 400 Error: bad request view

To 404, for example, 500,404 empathy
I have here created an application called booktest

  • Step1: modify settings.py

DEBUG = True - instead -> DEBUG = False
allowed_hosts = [] - changed -> ALLOWED_HOSTS = [ '*' ,]
Note: No changes will appear debug view, rather than an error view

  • Step2: Create a template file

Create a 404.html in booktest in templates directory:

 

 

Code:

<! DOCTYPE HTML>
<HTML>
<head>
<title> 404 </ title>
</ head>
<body>
Custom Processing 404
<hr>
page does not exist
</ body>
</ HTML>

  • Step3: Modify views.py file

Add Function page_not_found ()

 

 

def page_not_found(request):
return render(request, 'booktest/404.html')

  • Step4: urls.py under the modified project (frame comes)

Note: the application is not under urls.py, i.e. this is not: booktest / urls.py
add code:

 

 

= the urlpatterns [
URL (R & lt 'ADMIN ^ /', the include (admin.site.urls)),
URL (R & lt '^', the include ( 'booktest.urls')),
]
handler404 = "booktest.views.page_not_found"
i.e. : handler404 = ". application name .views function name"
Similarly:
handler500 =. "application name .views function name"
handler404 =. "application name .views function name"

Url a request does not exist:

 

 

----------------
Disclaimer: This article is the original article CSDN bloggers "GYT0313", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/qq_38038143/article/details/80105653

Guess you like

Origin www.cnblogs.com/cheng10/p/11775406.html