我试图通过传递用户名来使用flask的url_for()导航到用户的页面.我把html文件作为
{% for user in users %}
<li><h2>{{ user.name }}</h2>{{ user.email|safe }}
<a href="{{ url_for('users_stats',username = {{ user.name }}) }}">stats</a> #This doesn't work
<a href="/users/{{ user.name }}"> stats </a> #This does work
Run Code Online (Sandbox Code Playgroud)
在我的python文件中,我有函数as
@app.route('/users/<username>')
def users_stats(username):
...
Run Code Online (Sandbox Code Playgroud)
我在其中呈现另一个模板.为什么第一种方式不起作用?此外,由于第二种方式是通常的做法是做这样的网址或使用url_for()?
而不是这个:
<a href="{{ url_for('users_stats',username = {{ user.name }}) }}">stats</a>
Run Code Online (Sandbox Code Playgroud)
做这个:
<a href="{{ url_for('users_stats',username = user.name) }}">stats</a>
Run Code Online (Sandbox Code Playgroud)
您已经位于{{和}}阻止内部,因此您无需添加其中的第二级.