jinja2

Why does Jinja escape html in a macro?

ⅰ亾dé卋堺 提交于 2020-01-05 05:46:09
问题 I'm writing a Jinja macro to render some form data in a template, but for some reason the form data renders as escaped text instead of html! Here is my macro, with the first row of the form called: {% macro formrow(field) %} <tr> <td>form.{{ field }}.label|safe</td> <td>form.{{ field }}|safe</td> <td>form.{{ field }}.help_text|safe</td> <td>form.{{ field }}.errors|safe</td> </tr> {% endmacro %} {{ formrow('item_name') }} Ideas? What am I missing? 回答1: It seems you are not getting the Jinja

Custom parameter in Flask-WTFoms field

浪尽此生 提交于 2020-01-05 05:27:10
问题 forms.py my_field = TextField(u"Enter a number", validators=[Required('This Field is Required')]) my_form.html <table> <tr> <td colspan="4"> {{form.my_field(placeholder= form.my_field.label.text, class_="form-control")}} {% for error in form.my_field.errors %} <span>{{error}}</span> {% endfor %} </td> </tr> </table> This is a simple code. But I want to follow kind of this to render all the input fields in a loop instead of writing the code for each of them. But each field will have a

How to get ALL undefined variables from a Jinja2 template?

爷,独闯天下 提交于 2020-01-04 06:36:50
问题 I am trying to get all undefined variables from a Jinja2 template. Assume that I have a template like below. tmpstr = """ {% for row in csv %} sample {{row.field1}} stuff {{row.field2}} morestuff {{row.field3}} {% endfor %} """ and input dictionary as below cxt = {'csv': [ {'field3': 1234, 'field4': 12314}, {'field3': 2222, 'field4': 1213} ]} Here is how I try to render it. env = Environment(undefined=Undefined) tmp = env.from_string(tmpstr) tmpsrc = tmp.render(cxt) print(tmpsrc) Template

Adding jinja template dynamically

此生再无相见时 提交于 2020-01-04 05:56:18
问题 I have a jinja template that is the only content inside a set of div tags. <div id="section0"> {% include 'temppage.html' %} </div> When I press a button, I want to replace everything between the tags with something else. I was hoping to replace it with another jinja template, "{% include 'realpage.html' %}", but first I am unsure of how to replace the entire section, instead of just replacing a single word. Second, can I even add a jinja template dynamically, or do I need replace it with a

Get first “N” elements of a list in Jinja2 template in Ansible

北城以北 提交于 2020-01-03 19:19:43
问题 Most of my locations have 4+ DNS sources, but a few have less. Each location gets their own dns4_ips list variable like this: dns4_ips: - dns_A - dns_B - dns_C - dns_C My resolv.conf template looks like this: domain example.com search example.com dom2.example.com dom3.example.com {% for nameserver in (dns4_ips|shuffle(seed=inventory_hostname)) %} nameserver {{nameserver}} {% endfor %} The Jinja for loop works great, but in the cases where I have numerous nameservers I'd rather only list the

Get first “N” elements of a list in Jinja2 template in Ansible

大城市里の小女人 提交于 2020-01-03 19:19:31
问题 Most of my locations have 4+ DNS sources, but a few have less. Each location gets their own dns4_ips list variable like this: dns4_ips: - dns_A - dns_B - dns_C - dns_C My resolv.conf template looks like this: domain example.com search example.com dom2.example.com dom3.example.com {% for nameserver in (dns4_ips|shuffle(seed=inventory_hostname)) %} nameserver {{nameserver}} {% endfor %} The Jinja for loop works great, but in the cases where I have numerous nameservers I'd rather only list the

Raise error for undefined attributes in Jinja templates in Flask

穿精又带淫゛_ 提交于 2020-01-03 16:49:22
问题 By default Flask renders empty values for undefined attributes in Jinja templates. I want to raise an error instead. How can I change this behavior in Flask? Hello, {{ name }}! render_template('index.html') Hello, ! 回答1: Change the Flask app's Jinja env's undefined class to be StrictUndefined . from flask import Flask from jinja2 import StrictUndefined app = Flask(__name__) app.jinja_env.undefined = StrictUndefined If a template tries to use a variable that is undefined (except to test if it

Tidy for Jinja2 templates

冷暖自知 提交于 2020-01-03 07:18:50
问题 Is there a tidy-like tool or a tidy configuration, which works fine with Jinja2 templates? The default tidy has problems with Jinja code in attributes and Jinja-loops are formated in an unreadable way. My main requirement is to get the indentation level right. Everything else is nice to have, but not required. 回答1: You probably want a standalone tool and your question is old, so you've probably already solved it, but just in case this could be helpful. Some editors can handle jinja indent.

Python Flask flash not working correctly

我的梦境 提交于 2020-01-03 06:39:07
问题 I've simple 2 line code : @app.route('/contact/') def contact(): flash('We are reachable at ') return render_template('contact.html') I get the message 'We are reachable at' at /contact but it appears are normal text message. It doesn't background color(blue) or disappears after seconds. where contact.html contains {% extends "layout.html" %} {% block title %}Contact{% endblock title %} {% block body %} <h2> Contact Us </h2> Your email address must be valid as this is where reply will be sent

Variable within filename in jinja2 [duplicate]

隐身守侯 提交于 2020-01-03 05:34:13
问题 This question already has an answer here : Reference template variable within Jinja expression (1 answer) Closed 3 years ago . What is the right way to write something like this in jinja2 : {% for items in zipped %} <img src="{{ url_for('static', filename='img/{{items.logo}}') }}" /> Notice that items.logo is within another variable . 回答1: items.logo is already a variable. Try: {% for items in zipped %} <img src="{{ url_for('static', filename='img/' + items.logo) }}" /> {% endfor %} 来源: https