Flask url_for href not responding

Question:

I am trying to use url_for() to generate a url that will pass an argument to my Flask view. The client side html looks like this:

{% 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 %}

Which seems to generate the correct html, as the result looks like this:

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

And my flask view looks like this:

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

Now the odd part is that when I click on the ‘li’ element in the browser – nothing happens. There is no url change, flask doesn’t seem to recognise that the element was clicked at all i.e. it doesn’t print anything

BUT when I manually change the url to “accounts/8” for example, flask correctly returns “account id is: 8”

Can anyone spot why the html isn’t redirecting to the new flask view?

Asked By: user1960089

||

Answers:

You can’t set a href on a <li> element. It must go on the <a> tag inside.

Answered By: Daniel Roseman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.