【问题标题】:Flask url_for href not responding烧瓶 url_for href 没有响应
【发布时间】:2018-07-13 11:37:28
【问题描述】:

我正在尝试使用 url_for() 生成一个将参数传递给我的 Flask 视图的 url。客户端 html 如下所示:

{% for account in accounts %}
<li class="list-group-item text-link-hover account clickable-panel"
    href="{{ url_for('accounts', account_id=account.id) }}"
    account-id={{ account.id }}>
    <a>{{ account.name }}</a>
</li>
{% endfor %}

这似乎生成了正确的 html,结果如下所示:

<li class="list-group-item text-link-hover account clickable-panel selected" href="/accounts/27" account-id="27">
      <a>Account Name</a>
</li>

我的烧瓶视图如下所示:

@app.route('/accounts/<int:account_id>')
def accounts(account_id):
    print(account_id)
    return 'account id is: ' + str(account_id)

现在奇怪的是,当我在浏览器中单击“li”元素时 - 没有任何反应。没有 url 变化,flask 似乎根本没有识别出该元素被点击了,即它没有打印任何东西

但是,例如,当我手动将 url 更改为“accounts/8”时,flask 正确返回“account id is: 8”

谁能发现为什么 html 没有重定向到新的烧瓶视图?

【问题讨论】:

    标签: python html flask href url-for


    【解决方案1】:

    您不能在&lt;li&gt; 元素上设置href。它必须在里面的&lt;a&gt;标签上。

    【讨论】:

    • 哦哇...尴尬,这对我来说是个新闻。我很高兴我问了,因为我从来没有怀疑过。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2012-05-06
    相关资源
    最近更新 更多