Flask url_for 函数中的 Jinja 变量 [英] Jinja variables within the Flask url_for function

查看:30
本文介绍了Flask url_for 函数中的 Jinja 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种不同的方法,但无法让 URL 像我希望的那样显示为/item.我知道其他一切都正常运行,因为如果我简单地用字符串替换 {{item}},它工作正常.我在这里遗漏了什么吗?

这是问题所在的代码部分:

{% 用于名称中的项目 %}<div class="personlist"><a href={{ url_for('name', name = {{item}} ) }}><img id={{ item }} src="" alt={{ item }} width="40" height="40"></a>

<脚本>document.getElementById("{{ item }}").src = getName("{{ item }}");{% 结束为 %}<div>

谢谢

解决方案

您尝试在 {{}} 块中使用 {{}}.所以正确的用法是 {{ url_for('name', name=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

解决方案

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

这篇关于Flask url_for 函数中的 Jinja 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆