3、django輸入錯誤url,跳轉錯誤頁面
阿新 • • 發佈:2021-09-01
django404,500錯誤自定義頁面:
將
改為
1.修改settings檔案
DEBUG = False
ALLOWED_HOSTS = ['*'] 或者 ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
改成ALLOWED_HOSTS = ['*'] 就可以
2.配置urls檔案
from django.conf.urls import handler404, handler500
#錯誤頁面400
path('page_not_found/', views.page_not_found),
#錯誤頁面500
path('page_not_found/', views.page_not_found),
3.在views檔案中定義函式page_not_found和page_error
from django.shortcuts import render_to_response
def page_not_found(request):
return render_to_response('404.html')
def page_error(request):
return render_to_response('500.html')
4.在app的templates下建立404.html和500.html檔案(檔案內就是你自定義的404或者500頁面)
400.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="none"/>
<title>404 Not Found</title>
<style>
*{font-family:"Microsoft Yahei";margin:0;font-weight:lighter ;text-decoration:none;text-align:center;line-height:2.2em;}
html,body{height:100%;}
h1{font-size:100px;line-height:1em;}
table{width:100%;height:100%;border:0;}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<h1>404</h1>
<h3>大事不妙啦!</h3>
<p>你訪問的頁面好像不小心被作者給弄丟了~<br/>
<a href="/login/">請返回首頁 ></a> #這裡直接返回登入頁面
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
500.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>出錯了</title>
</head>
<body>
<h1>程式設計師又要加班了。。。。。</h1>
</body>
</html>