python - 为所有 Flask 路由添加前缀

标签 python routes flask

我有一个要添加到每条路由的前缀。现在我在每个定义的路径中添加一个常量。有没有办法自动执行此操作?

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/

相关文章:

python - 类型错误 : 'int' object does not support item assignment error

python - 重击 : syntax error near unexpected token `(' - Python

python - 在 pandas 数据框中将元素设置为 None

c - 获取网关以用于 ANSI C 中的给定 ip

python - 重定向后 Flask flash 不工作

Python 子进程管道 block

c# - 如何通过普通路由重定向

asp.net - MVC 区域路由 - 带文件夹的 Controller 文件夹

python - 为什么 Flask 中的应用程序上下文不是应用程序的单例?

Python Flask 和 Psycopg — 在 WHERE 中查询字符串 request.get