jinja2

Using variables across Flask routes [duplicate]

谁都会走 提交于 2019-12-19 04:12:48
问题 This question already has answers here : Are global variables thread safe in flask? How do I share data between requests? (2 answers) Closed last year . I am learning Flask and have a question regarding use of variables in the context of routes.For Example, my app.py: from flask import Flask, render_template app = Flask(__name__) @app.route("/index") def index(): a=3 b=4 c=a+b return render_template('index.html',c=c) @app.route("/dif") def dif(): d=c+a return render_template('dif.html',d=d)

Reuse a block of code in several places in jinja2

佐手、 提交于 2019-12-18 19:07:16
问题 I have this html snippet which needs to be used in lots of places in the jinja2 templates: <div class="usedalot">{{ somevalue }}</div> for example, in template1.html, template2.html. template3.html, this code is repeated several places <!-- template1.html, template2.html. template3.html --> <div class="usedalot">{{ somevalue }}</div> ...... <div class="usedalot">{{ somevalue }}</div> .... <div class="usedalot">{{ somevalue }}</div> ...... Instead of copying and pasting, is there someway to

JSON is appearing as unicode entities in Jinja2 template

心已入冬 提交于 2019-12-18 19:04:31
问题 I using Jinja2 with webapp2. Jinja2 encodes all 'context' data into unicode as their doc says. This is proving problematic when I try to insert a json string into the the template: jsonData = json.loads(get_the_file('catsJson.txt')) I pass jsonData to template and I'm able to loop it successfully but when I insert a json element into HTML, it looks like this: <option value='[u'dogs', u'cats']'> I want it to look like this (as it is in the original json string): <option value='["dogs", "cats"]

How can I break a for loop in jinja2?

依然范特西╮ 提交于 2019-12-18 18:50:56
问题 How can I break out of a for loop in jinja2? my code is like this: <a href="#"> {% for page in pages if page.tags['foo'] == bar %} {{page.title}} {% break %} {% endfor %} </a> I have more than one page that has this condition and I want to end the loop, once the condition has been met. 回答1: You can't use break , you'd filter instead. From the Jinja2 documentation on {% for %}: Unlike in Python it’s not possible to break or continue in a loop. You can however filter the sequence during

Testing for a List in Jinja2

送分小仙女□ 提交于 2019-12-18 18:41:46
问题 As far as I can see, there is no way to test if an object is a List instance in Jinja2. Firstly, is that correct and secondly, has anyone implemented a custom test/extension in Jinja2? Any help would be great. 回答1: You can easily do this whit a custom filter in jinja2. First create you test method: def is_list(value): return isinstance(value, list) And add it as an custom filter: j = jinja2.Jinja2(app) j.environment.filters.update({ 'is_list': is_list, }) 回答2: I did it like this: {% if var is

Import javascript files with jinja from static folder [duplicate]

那年仲夏 提交于 2019-12-18 15:54:55
问题 This question already has answers here : Link to Flask static files with url_for (2 answers) Pass parameter with Python Flask in external Javascript (2 answers) Closed 2 years ago . I need to have access to jinja template in javascript files (due to i18n tags). So the way that i found is just load the js file with include, from the jinja method. {% include "file.js" %} However this method will look for files only in templates folder. But the js files must be in the static folder. My question

Passing a JSON object from Flask to JavaScript

不想你离开。 提交于 2019-12-18 12:28:44
问题 I'm having troubles getting a Flask/Python variable passed to Javascript. Basically, I'm importing from MySQL and have tried rendering the return in three different ways. (43.8934276, -103.3690243), (47.052060, -91.639868), (45.1118, -95.0396) that is the output when my dict item has the following ran on it. new_list = [tuple(d.values()) for d in MySQL_Dict] output = ', '.join('(' + ', '.join(i) + ')' for i in new_list) This method is no good, but I added it for detail, it's not in the right

Case statement for setting var in Ansible/Jinja2

只谈情不闲聊 提交于 2019-12-18 12:18:58
问题 I'm using Ansible with Jinja2 templates, and this is a scenario that I can't find a solution for in Ansible's documentation or googling around for Jinja2 examples. Here's the logic that I want to achieve in Ansible: if {{ existing_ansible_var }} == "string1" new_ansible_var = "a" else if {{ existing_ansible_var }} == "string2" new_ansible_var = "b" <...> else new_ansible_var = "" I could probably do this by combining several techniques, the variable assignment from here: Set variable in jinja

What does this “-” in jinja2 template engine do?

大城市里の小女人 提交于 2019-12-18 11:45:36
问题 I am learning jinja2 because Google App Engine recommends it. I found this example on Wikipedia: http://en.wikipedia.org/wiki/Jinja_%28template_engine%29 {%- for item in item_list %} {{ item }}{% if not loop.last %},{% endif %} {%- endfor %} What is the "-" in "{%- for"? Also, where can I find jinja2 examples (better with Google App Engine)? Thanks a lot! 回答1: It suppresses extra vertical spacing, commonly used when you don't want excessive spacing between elements you're looping through. If

casting ints to str in Jinja2

自作多情 提交于 2019-12-18 11:42:09
问题 I want to cast an int that's passed to the template through the url, but it says that the str function isn't defined. How do I get around this? Here's my code: {% extends "base.html" %} {% block content %} {% for post in posts %} {% set year = post.date.year %} {% set month = post.date.month %} {% set day = post.date.day %} {% set p = str(year) + '/' + str(month) + '/' + str(day) + '/' + post.slug %} <h3> <a href="{{ url_for('get_post', ID=p) }}"> {{ post.title }} </a> </h3> <p>{{ post