Jinja表达式中的引用模板变量 [英] Reference template variable within Jinja expression

查看:92
本文介绍了Jinja表达式中的引用模板变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ app.route('/ magic /< filename>') 
def moremagic(filename):
pass

现在在一个模板中,我想用 url_for()来调用这个路由,如下所示:

 < h1>您上传了{{name}}< h1> 
< a href ={{url_for('/ magic /< filename>))}>点击查看魔术发生< / a>

我试过了:

 < a href ={{url_for('/ magic',filename = {{name}})}}>点击查看magic happen< / a> ; 

抛出 jinja2.TemplateSyntaxError:expected token':'got}



任何人都可以建议如何取得 {{name}} 模板到 url_for()中,这样当我点击我时,调用正确的 app.route

解决方案 <{code> {{...}} 中的所有内容都是类似Python的表达式。你不需要在里面使用另一个 {{...}} 来引用变量。



额外的括号:

< h1> you uploaded {{name}}< h1>
< a href ={{url_for('/ magic',filename = name)}}>点击查看magic happen< / a>


I have a route defined like this:

@app.route('/magic/<filename>')
def moremagic(filename):
    pass

And now in a template I want to call that route using url_for() like so:

<h1>you uploaded {{ name }}<h1>
<a href="{{ url_for('/magic/<filename>') }}">Click to see magic happen</a>

I have tried:

<a href="{{ url_for('/magic', filename={{ name }}) }}">Click to see magic happen</a>

That throws a jinja2.TemplateSyntaxError: expected token ':' got }

Can anyone suggest how to get the {{ name }} that appears in the template into the url_for() so that when I click I call the correct app.route?

解决方案

Everything inside the {{ ... }} is a Python-like expression. You don't need to use another {{ ... }} inside that to reference variables.

Drop the extra brackets:

<h1>you uploaded {{ name }}<h1>
<a href="{{ url_for('/magic', filename=name) }}">Click to see magic happen</a>

这篇关于Jinja表达式中的引用模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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