我有一个要添加到每条路由的前缀。现在我在每个定义的路径中添加一个常量。有没有办法自动执行此操作?
PREFIX = "/abc/123"
@app.route(PREFIX + "/")
def index_page():
return "This is a website about burritos"
@app.route(PREFIX + "/about")
def about_page():
return "This is a website about burritos"
最佳答案
您可以将路线放入蓝图中:
bp = Blueprint('burritos', __name__,
template_folder='templates')
@bp.route("/")
def index_page():
return "This is a website about burritos"
@bp.route("/about")
def about_page():
return "This is a website about burritos"
然后使用前缀向应用程序注册蓝图:
app = Flask(__name__)
app.register_blueprint(bp, url_prefix='/abc/123')
关于python - 为所有 Flask 路由添加前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38856539/