Jinja变量在Flask url_for函数中

问题描述:

我尝试了多种不同的东西,但无法像我想要的那样将URL显示为/ item。我知道其他一切都正常运行,因为如果我简单地用一个字符串替换{{item}},它可以正常工作。我在这里丢失了什么?

I have tried multiple different things, but cannot get the URL to appear as /item like I want it to. I know that everything else is functioning correctly because if I simply replace {{item}} with a string, it works fine. Am I missing something here?

这里是问题所在的代码段:

Here is the code section where the problem lies:

<div class="person_images_group">
    {% for item in names %}
        <div class="personlist">
            <a href={{ url_for('name', name = {{item}} ) }}>
                <img id={{ item }} src="" alt={{ item }} width="40" height="40">
            </a>
        </div>
        <script>
            document.getElementById("{{ item }}").src = getName("{{ item }}");
        </script>
    {% endfor %}
<div>

谢谢

Thank you

您可以在 {{}} 块中使用 {{}} 所以正确的用法是 {{url_for('name',name = item)}}

You try use {{}} in {{}} block. So right usage is {{ url_for('name', name=item) }}.