from flask import Flask, render_template, abort app = Flask(__name__) @app.errorhandler(404) def errorhandler(e): return render_template("404.html") @app.route("/user/<user_id>") def get_user(user_id): if int(user_id) == 1: return render_template("user.html") else: abort(404) if __name__ == '__main__': app.run(debug=True) #404.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>抱歉,你所查找的页面不存在</h1> <h2>你一定是在逗我吧</h2> </body> </html> #user.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>user</h1> </body> </html>