jinja2

How do I access Jinja2 for loop variables outside the loop?

旧时模样 提交于 2020-01-15 11:24:53
问题 I have a Jinja2 template page which contains two separate {% for %} loops. If neither of these loops contain any items, I want the page to redirect. I'm trying to do something like this (pseudo-code): loop1 = loop.length (in first loop) loop2 = loop.length (in second loop) if loop1 + loop2 == 0: redirect # (outside both loops) Is this even possible? Is there a way to make the loop.length variables available outside their respective loops? 回答1: You can check your lists for truth, empty lists

How do I access Jinja2 for loop variables outside the loop?

独自空忆成欢 提交于 2020-01-15 11:24:11
问题 I have a Jinja2 template page which contains two separate {% for %} loops. If neither of these loops contain any items, I want the page to redirect. I'm trying to do something like this (pseudo-code): loop1 = loop.length (in first loop) loop2 = loop.length (in second loop) if loop1 + loop2 == 0: redirect # (outside both loops) Is this even possible? Is there a way to make the loop.length variables available outside their respective loops? 回答1: You can check your lists for truth, empty lists

serving i18n js using babel, django, & jinja2

▼魔方 西西 提交于 2020-01-15 10:43:37
问题 Using django, with jinja2 for rendering & babel for message extraction I have some js files that need to be internationalized. I haven't been able to figure out a syntax for extracting messages from them which would also let jinja2 render them. Either jinja2 has to learn to read an extractable syntax, or I have to extract from something jinja2 can render. (Or, do this another way entirely) Extracting If I mark messages in the js with gettext('message') It extracts just fine. Rendering But

JQuery to reload DIV? (Flask/Jinja2)

孤街醉人 提交于 2020-01-15 10:19:06
问题 I've searched to no avail.. I guess my question is a bit unique. I have a DIV with some content in it, that content get's brought in currently by passing the object of a SQL query into the template. That works fine, but doesn't allow me to update the content every x seconds, without refreshing the entire page. Something I don't want to do. So I wanted to know I could force JQuery to reload my DIV element? Would this also reload the code in it? Because that would force my div to update.. Here

How to download a file in Python (Jinja2) on-click Export button?

我怕爱的太早我们不能终老 提交于 2020-01-15 06:56:26
问题 I have a button export : <button class="aptButton" formaction="/export/" type="submit">export</button> and I have this in the /export/ index.cgi #! /apollo/sbin/envroot $ENVROOT/bin/python # -*- coding: utf-8 -*- import cgitb cgitb.enable() import cgi def main(): print "Content-Type: text/html" print form = cgi.FieldStorage() results = helpers.getResults() environment = helpers.get_environment() print environment.get_template('export.html').render( results = results) main() and I have this in

Blocks in included files not being filled by extended templates

淺唱寂寞╮ 提交于 2020-01-15 03:07:20
问题 I have a template that looks like this: {% include "base/top.html" with context %} {% include "base/nav.html" with context %} <div id="content"> Stuff {% block content %}{% endblock %} </div> {% include "base/bottom.html" with context %} base/nav.html and base/bottom.html contain static content, but base/top.html contains a {% block title %} . So when I have a second template as that attempts to inherit from the first file like so: {% extends firstfile.html %} {% block title %}Imarealpage!{%

Embed png image in html file using Jinja2

落花浮王杯 提交于 2020-01-13 23:55:14
问题 I'm using Jinja2 to create html reports as a single html file. I would like to include the company logo inside the html file rather than link to it; in this way the logo is displayed even if the person reading the report is offline. Can this be automatically done using Jinja2? If not how can I embed a png image in the html manually? 回答1: look for a Base64 encoder on Google (e.g. http://www.opinionatedgeek.com/dotnet/tools/base64encode/). After that you can insert <img src="data:image/png

UTC to local timezone in a flask/jinja template

别来无恙 提交于 2020-01-13 14:04:53
问题 I know this has been asked before, and I'm still pulling my hair out trying to figure this one out. I've tried pytz, dateutil, and now flask_moment. Still having problems converting a MySQL table datetime, which is UTC, to display in the jinja2 template as local time, specifically UTC - 05:00 (EST). My jinja2 for loop looks like this: {% for data in items %} ... <td>{{data.loggedInBy}}</td> <td>{{data.timeIn.strftime('%I:%M %p')}}</td> ... {% endfor %} I'm new to python/flask/jinja, so go

UTC to local timezone in a flask/jinja template

倾然丶 夕夏残阳落幕 提交于 2020-01-13 14:04:43
问题 I know this has been asked before, and I'm still pulling my hair out trying to figure this one out. I've tried pytz, dateutil, and now flask_moment. Still having problems converting a MySQL table datetime, which is UTC, to display in the jinja2 template as local time, specifically UTC - 05:00 (EST). My jinja2 for loop looks like this: {% for data in items %} ... <td>{{data.loggedInBy}}</td> <td>{{data.timeIn.strftime('%I:%M %p')}}</td> ... {% endfor %} I'm new to python/flask/jinja, so go

Jinja, Flask and WTForms: how to pass parameters in field? [duplicate]

走远了吗. 提交于 2020-01-13 11:28:09
问题 This question already has answers here : Using Flask-WTForms, how do I style my form section of the html? (2 answers) Closed 3 years ago . I am following this tutorial http://flask.pocoo.org/docs/0.10/patterns/wtforms/ Here’s an example _formhelpers.html template with a macro: {% macro render_field(field) %} <dt>{{ field.label }} <dd>{{ field(**kwargs)|safe }} {% if field.errors %} <ul class=errors> {% for error in field.errors %} <li>{{ error }}</li> {% endfor %} </ul> {% endif %} </dd> {%