Answer a question

I am creating a link in a Jinja2 template using Flask and I am running into a problem. I want to access a variable passed to the template within a code block, but I am unsure how to do it or if it is even possible. What I am trying to do:

<a href="{{ url_for('myRoute', varToBePassed = {{templateVar}} ) }}" >

So basically I want to pass a query parameter to a variable using the url_for function in a template, but it doesn't like the nested double curly braces. I could alternatively just do it like:

<a href="/myRoute/{{templateVar}}" >

which works, but I feel like I could run into problems in the future if I have to move things in the file structure.

Is there a way to nest curly braces in Jinja templates? Or another way to pass variables to code blocks in Jinja? I looked into macros, but it seemed like I would run into the same problems with nested curly braces in it, as well.

Answers

Just use the template variable directly:

a href="{{ url_for('myRoute', varToBePassed = templateVar ) }}" >
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐