Django 简明教程
Django - Page Not Found (404)
What is 404 Page Not Found Error?
HTTP 协议定义了各种状态代码来指示不同类型的 HTTP 响应。 “404” 是当服务器找不到请求的网页时对应的 HTTP 状态代码。它被称为 “404 错误”,也称为 “404 未找到错误” 或 “HTTP 404 未找到”。
The HTTP protocol defines various status codes to indicate different types of HTTP responses. "404" is the HTTP status code that corresponds to the situation when the server cannot find the requested webpage. It is called as "404 error", also known as the "404 Not Found error" or "HTTP 404 Not Found".
Django’s Default Template for 404 Error
在 Django 中,某个 URL 路由会映射到某个视图。当 URL 没有对应的视图时,可能会出现 404 错误。
In Django, a given URL route is mapped to a certain view. The 404 error may appear when a URL doesn’t have a corresponding view.
让我们输入一个在 Django 项目的 URLCONF 中未定义的 URL 路由 −
Let us enter a URL route that has not been defined in the URLCONF of the Django project −
使用 startproject 模板创建的 Django 项目,将 DEBUG 参数设置为 TRUE。当找不到视图,并且 DEBUG 设置为 TRUE 时,将显示以上页面。这是 Django 404 错误代码的默认模板。
The Django project, created with the startproject template, has the DEBUG parameter set to TRUE. The above page appears when a view isn’t found and DEBUG is set to TRUE. This is Django’s default template for 404 error code.
Render a Custom Error Page
若要呈现自定义错误页面,请在 "setings.py" 模块中将 DEBUG 参数设置为 FALSE。此外,你需要指定 ALLOWED_HOSTS 列表,例如 localhost 或某个域,如 https://example.com 。将此参数设置为 "*" 以适用于任何主机名。
To render a custom error page, set the DEBUG parameter to FALSE in the "setings.py" module. Also, you need to specify the list of ALLOWED_HOSTS such as localhost or a certain domain such as https://example.com. Set this parameter to "*" for any hostname.
DEBUG = False
ALLOWED_HOSTS = ["*"]
在进行这些更改之后,同样的 URL 不会显示任何 DEBUG 消息,如前一幅图中那样。
After these changes, the same URL doesn’t show any DEBUG message as in the earlier figure.
你可以进一步设计一个自定义模板,将其命名为 "404.html",并将其放置在 "BASE_DIR/template" 文件夹中。
You can further design a customised template, name it as "404.html", and place it in the "BASE_DIR/template" folder.
404.html
<html>
<body>
<h2 style="text-align: center; color: blue;
font-weight:900;">The Page is Not found</h2>
</body>
</html>
现在,只要未找到视图,就会显示此页面。
Now, this page is displayed whenever the view is not found.
自定义 404 错误响应的另一种方法是在项目文件夹下的 " views.py " 文件中定义 handler404() view 。
Another approach to customize the 404 error response is to define a handler404() view in "views.py" file under the project folder.
Note − view.py 模块默认位于 app 文件夹中。你需要在项目文件夹中显式创建相同的模块。
Note − The view.py module is situated by default in the app folder. You need to create the same in the project folder explicitly.
views.py
from django.shortcuts import render
def handler404(request, exception):
return render(request, '404handler.html')
然后,通过在项目的 URLCONF 中将 handler404 variable 分配给 handler404() function ,指导 Django 在找不到视图时呈现此模板。
Then, direct Django to render this template whenever a view isn’t found by assigning the handler404 variable with handler404() function, in the URLCONF of the project.
urls.py
from django.contrib import admin
from django.urls import path
from . import views
handler404 = views.handler404
urlpatterns = [
path('admin/', admin.site.urls),
]
404handler.html
将以下 HTML 脚本另存为 templates 文件夹中的 404handler.html −
Save the following HTML script as 404handler.html in the templates folder −
<html>
<body>
<h2 style="text-align: center; color: blue;
font-weight:900;">The Page is Not found</h2>
<br>
<br>
<a href="../home"><b>Back to Home</b></a>
</body>
</html>
访问任何未定义的 URL 路由以呈现此自定义的 404 错误页面。
Visit any undefined URL route to render this customized 404 error page.