jinja2

How can I break the with_items loop based on a condition

和自甴很熟 提交于 2019-12-23 20:27:33
问题 I want to break out of the with_items loop based on a condition. That condition for arguments sake is if the stdout of a command is equal to a particular string. Obviously the example below does not work but this is an idea of what I want to do. For example: - name: testing loop shell: "echo {{ item }}" with_items: - "one" - "two" - "three" register: shell_command # registering the shell command and it's attributes when: shell_command.stdout == "two" # break once the stdout of the run shell

Flask - How to use CSS when i'm using Response(stream_template) with generator

纵饮孤独 提交于 2019-12-23 18:17:24
问题 I'm trying to learn Flask and making a small app. So at first, I tested without a css file by using: (delay() gets the result from generator) return Response(stream_template('login.html', data=delay())) It works fine for me then now i want to implement new css, let's called it style.css and i put it in the static folder. In the html file I have: <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"/> The code will probably won't work as there is a problem with context

Get Flask to show image not located in the static directory

冷暖自知 提交于 2019-12-23 14:56:34
问题 I'm trying to display images located in a folder which is not the default static folder. My template looks like: {% extends "layout.html" %} {% block body %} Albums {% for album in albums %} <img src="{{ album ['ThumbPath'] }}"> {% endfor %} {% endblock %} The generated HTML looks like: <!doctype html> <title>iPhoto</title> <link rel=stylesheet type=text/css href="/static/style.css"> <div class=page> <h1>iPhoto</h1> <img src="/Users/Darrell/Pictures/iPhoto Library/Thumbnails/2013/08/09

Get Flask to show image not located in the static directory

烈酒焚心 提交于 2019-12-23 14:55:24
问题 I'm trying to display images located in a folder which is not the default static folder. My template looks like: {% extends "layout.html" %} {% block body %} Albums {% for album in albums %} <img src="{{ album ['ThumbPath'] }}"> {% endfor %} {% endblock %} The generated HTML looks like: <!doctype html> <title>iPhoto</title> <link rel=stylesheet type=text/css href="/static/style.css"> <div class=page> <h1>iPhoto</h1> <img src="/Users/Darrell/Pictures/iPhoto Library/Thumbnails/2013/08/09

Compare version numbers using jinja2

孤者浪人 提交于 2019-12-23 12:01:29
问题 I am using jinja2 template to install/upgrade packages. The logic was setting a variable for current installed version and compare it with the available version. It was working fine but once we passed in to 10.x, comparison quit working. Is it possible to cast the variable so it can correctly identify 10.9.8 is greater than 9.8.7? Thanks current_version=['9.8.7'] {% if current_version < '10.9.8' %} 回答1: There's a special test version_compare: {% if current_version | version_compare('10.9.8',

Using jinja to send data to Javascript

♀尐吖头ヾ 提交于 2019-12-23 10:55:00
问题 I have Python code, in which I'm using jinja to send data to a template in Flask. I can access the code just find in HTML, but when I try displaying the data in Javascript, it doesn't work. For example, here's my Python code: name = "Steve" return render_template('simple.html',data=json.dumps(name)) And in my simple.html code, in the html body: <script> var name = {{ data }}; alert(name); </script> The error in my console says "SyntaxError: Unexpected token '&'" I know I've seen this problem

jinja2.exceptions.TemplateNotFound: bootstrap/base.html

社会主义新天地 提交于 2019-12-23 10:54:32
问题 I'm inheriting bootstrap/base.html in a Flask application after installing the Bootstrap Flask extension but having the below error: jinja2.exceptions.TemplateNotFound: bootstrap/base.html 回答1: You need to import Bootstrap in this way below: from flask_bootstrap import Bootstrap ... bootstrap = Bootstrap(app) flask.ext.bootstrap is deprecated 回答2: Need to import Bootstrap from flask.ext.bootstrap and bootstrap the flask app as below: from flask.ext.bootstrap import Bootstrap ... bootstrap =

render html strings in flask templates

风格不统一 提交于 2019-12-23 09:20:45
问题 I'm building a web page to show articles. In my database, I have to attributes, one of which is used to put Markdown code and the other to save HTML code converted from the Markdown code. I want to get the HTML and add it to my base HTML. I use Flask framework and SQLAlchemy and the data is saved in sqlite database. My model: class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) body = db.Column(db.String) timestamp = db.Column(db.String) tag = db

Load YAML nested with Jinja2 in Python

天大地大妈咪最大 提交于 2019-12-23 09:18:17
问题 I have a YAML file ( all.yaml ) that looks like: ... var1: val1 var2: val2 var3: {{var1}}-{{var2}}.txt ... If I load it in Python like this: import yaml f = open('all.yaml') dataMap = yaml.safe_load(f) f.close() print(dataMap["var3"]) the output is {{var1}}-{{var2}}.txt and not val1-val2.txt . Is it possible to replace the nested vars with the value? I tried to load it with: import jinja2 templateLoader = jinja2.FileSystemLoader( searchpath="/path/to/dir" ) templateEnv = jinja2.Environment(

Is it possible to access the key_name of an object from inside a jinja2 template?

ε祈祈猫儿з 提交于 2019-12-23 07:47:55
问题 I'm using python google app engine with the webapp framework, substituting jinja2 templates for django templates. I know you can get the key_name of an object from inside a handler method by calling it like so: goody = object.key().name() But is it possible to access the key_name from within the template logic? Something like this? {% for object in objectList %} {{object|key_name}} {% endfor %} Obviously that doesn't work, but is there some filter or hidden attribute I can use to get at the