【问题标题】:cannot pass variable with url_for flask, python, html5无法使用 url_for flask、python、html5 传递变量
【发布时间】:2020-01-17 19:07:02
【问题描述】:

我试图通过 url_for (flask, python3) 在一些文本中放置一个链接,并传递一个变量,但我尝试过的没有任何效果。这是我使用的代码。难道我做错了什么? html5:

<a class="mr-2" href={{ url_for('user','username'=posts.author)}} >TEXT</a>

python3:

@app.route('/user')
def user(username):
    print(username)

【问题讨论】:

    标签: python html python-3.x flask url-for


    【解决方案1】:

    您需要将模板代码更改为如下所示:

    <a class="mr-2" href="{{ url_for('user',username=posts.author) }}" >TEXT</a>
    

    但是,您还必须更改 python 代码才能使其有效:

    @app.route('/user/<username>')
    def user(username):
        print(username)
    

    一个有效的请求现在是:

    /user/CodEdo
    

    假设模板中的posts.authorCodEdo,那么应该在href 中呈现哪个URL。

    【讨论】:

      猜你喜欢
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2018-05-31
      • 1970-01-01
      • 2018-03-05
      • 2020-11-29
      相关资源
      最近更新 更多