【问题标题】:Redirect to same page but add dynamic route重定向到同一页面但添加动态路由
【发布时间】:2020-10-06 14:23:22
【问题描述】:

我正在构建一个 web 应用程序,您可以在其中将 Player-Profiles 添加到表格中以进行比较。我已将所有更改发布到同一个 URL,现在我发现所有用户都可以看到这些更改。

我考虑过如何解决这个问题:我尝试为添加到表格中的每个配置文件个性化 URL,而不是 url .../stats,例如 .../stats?id=237(对于一个配置文件).../stats?id=237id=335(对于两个配置文件)。

统计方法:

@app.route('/stats/', methods=['POST', 'GET'])
def pstats():
    ...
    ...
    urla= [237, 335]
    return redirect(url_for('pstats', data = urla ))
  1. 路线:.../stats
  2. 路线:.../stats?id=237
  3. 路线:.../stats?id=237id=335

如何动态编写pstats 的路由,使其看起来像 1. route 或 2. route?

【问题讨论】:

    标签: flask url routes


    【解决方案1】:

    使用request.args 获取传递给您的路由的查询参数。

    @app.route('/stats/', methods=['POST', 'GET'])
    def pstats():
        id = request.args['id']
    

    id 传递给url_for 以生成包含查询参数的URL。

    url_for(url_for('pstats', id=id))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-04
      • 2021-02-28
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2012-07-22
      • 2018-12-04
      相关资源
      最近更新 更多