📜  Django – 创建 404 错误页面

📅  最后修改于: 2022-05-13 01:54:44.456000             🧑  作者: Mango

Django – 创建 404 错误页面

当调用的 URL 不存在或尚未定义时,将引发404 错误。这就是通常所说的页面不存在错误。为了处理来自网站中未定义 URL 的请求,会创建一个错误页面。如果用户请求这些类型的 URL,则执行此错误页面以向用户反映该页面不存在,您可以进一步将它们重定向到另一个 URL,例如主页。使用这种技术,我们还可以改善我们网站的 SEO。

在本文中,我们将为我们的应用程序创建一个简单的 404 错误页面。

执行:

  • 首先,转到url.py 您的应用程序文件并输入以下代码:
Python3
from django.contrib import admin
from django.urls import include, path
 
# add the following path in the
# url patterns
urlpatterns = [
    path('', include('pages.urls'))
]
 
# add a flag for
# handling the 404 error
handler404 = 'pages.views.error_404_view'


Python3
from django.conf import settings
from django.shortcuts import redirect
 
 
def error_404_view(request, exception):
   
    # we add the path to the the 404.html file
    # here. The name of our HTML file is 404.html
    return render(request, '404.html')


HTML
{% load static %}


   
      
      
         
         
            
            
            Web Developer
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
         
         
            
            
            
            
            {% comment %}
            
               
                                  
               Menu                
             {% endcomment %}                          {% comment %}                          {% endcomment %}                          
               
                  
                  
                     
                        
                           

404 Error.

                              

                              Oooooops! Looks like nothing was found at this location.                               Maybe try on of the links below, click on the top menu                               or try a search?                            

                               {% comment %}                                                        {% endcomment %}                         
                                                                     
                                                            
                                     
                     
                                                                       
                                                             
               
                            
                         
               
            
                                                                    


  • 之后打开应用程序的view.py并添加以下代码:

Python3

from django.conf import settings
from django.shortcuts import redirect
 
 
def error_404_view(request, exception):
   
    # we add the path to the the 404.html file
    # here. The name of our HTML file is 404.html
    return render(request, '404.html')
  • 之后,您只需将 HTML 页面添加到您的模板页面,您的工作就完成了。

HTML

{% load static %}


   
      
      
         
         
            
            
            Web Developer
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
         
         
            
            
            
            
            {% comment %}
            
               
                                  
               Menu                
             {% endcomment %}                          {% comment %}                          {% endcomment %}                          
               
                  
                  
                     
                        
                           

404 Error.

                              

                              Oooooops! Looks like nothing was found at this location.                               Maybe try on of the links below, click on the top menu                               or try a search?                            

                               {% comment %}                                                        {% endcomment %}                         
                                                                     
                                                            
                                     
                     
                                                                       
                                                             
               
                            
                         
               
            
                                                                    

现在,每当出现 404 错误时,它都会向他们显示 404.html 文件。:

输出 :

注意:只有当您使该网站在主机上运行或将调试模式设置为“False”并设置允许的主机时,您才能看到此页面的输出。