外观
自定义404页面
使用app.errorhandler()装饰器就可以将一个函数注册为返回404页面的视图函数。
如自定义一个404.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>页面未找到</title>
<style>
*{
margin: 0;
padding: 0;
}
.container{
display: flex;
width: 100%;
height: 100vh;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div class="container">
<div>
<h1>页面未找到</h1>
<p>你要的资源未找到,回到主页上看看吧</p>
<p><a href="#">回到主页</a></p></div>
</div>
</body>
</html>然后创建一个run.py:
from flask import Flask, render_template
app = Flask(__name__)
@app.errorhandler(404)
def error_page(e):
return render_template('404.html')
@app.route('/')
def index():
return '首页'
if __name__ == '__main__':
app.run()运行run.py,然后在浏览器中输入不存在的URL,就会返回404.html了。