【问题标题】:How to fix a url_prefix in flask app?如何修复烧瓶应用程序中的 url_prefix?
【发布时间】:2018-07-19 11:14:40
【问题描述】:

我有一个带有多个模糊打印的烧瓶应用程序。他们是auth,admin,user,sample,data。 在注册蓝图时,我为 admin 蓝图添加了 url_prefix。但是我喜欢将 url 前缀保留为 admin,即使用户(管理员用户)正在访问除 auth 以外的任何视图,也将 user 作为普通用户访问的所有视图的 url 前缀,而不是 @ 987654326@.

@admin.route('/dashboard')
def index():
    return render_template('dashboard.html')

这会给我:localip:5000/admin/dashboard 但即使我正在访问不同蓝图的视图,我也需要拥有 localip:5000/admin/sample

 @admin.route('/sample')
    def samples():
        return redirect(url_for('sample.index'))



@sample.route('/sample')
def index():
    return render_template('sample.html')

现在我得到了localip:5000/sample,但我需要localip:5000/admin/samplelocalip:5000/user/sample,具体取决于用户角色。

【问题讨论】:

    标签: python flask routing blueprint


    【解决方案1】:

    您是否尝试过将用户角色作为 url 参数 like they show here 传递,您似乎应该能够发送当前用户的角色以及您发送的任何请求。

    @blueprint.route('/<user_role>/dashboard')
    def dashboard(user_role):
    
    
        return render_template('dashboard.html')
    

    当您重定向到另一个页面时,只需始终传递用户角色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2019-12-05
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多