jinja2

Is there a direct approach to format numbers in jinja2?

隐身守侯 提交于 2019-12-18 10:54:24
问题 I need to format decimal numbers in jinja2. When I need to format dates, I call the strftime() method in my template, like this: {{ somedate.strftime('%Y-%m-%d') }} I wonder if there is a similar approach to do this over numbers. Thanks in advance! 回答1: You can do it simply like this, the Python way: {{ '%04d' % 42 }} {{ 'Number: %d' % variable }} Or using that method: {{ '%d' | format(42) }} I personally prefer the first one since it's exactly like in Python. 回答2: I want to highlight Joran

Python jinja2 shorthand conditional

﹥>﹥吖頭↗ 提交于 2019-12-18 10:31:36
问题 Say I have this: {% if files %} Update {% else %} Continue {% endif %} In PHP, say, I can write a shorthand conditional, like: <?php echo $foo ? 'yes' : 'no'; ?> Is there then a way I can translate this to work in a jinja2 template: 'yes' if foo else 'no' 回答1: Yes, it's possible to use inline if-expressions: {{ 'Update' if files else 'Continue' }} 回答2: Alternative way (but it's not python style. It's JS style) {{ files and 'Update' or 'Continue' }} 来源: https://stackoverflow.com/questions

How to output a comma delimited list in jinja python template?

此生再无相见时 提交于 2019-12-18 09:56:09
问题 If I have a list of users say ["Sam", "Bob", "Joe"] , I want to do something where I can output in my jinja template file: {% for user in userlist %} <a href="/profile/{{ user }}/">{{ user }}</a> {% if !loop.last %} , {% endif %} {% endfor %} I want to make the output template be: Sam, Bob, Joe I tried the above code to check if it was on the last iteration of the loop and if not, then don't insert a comma, but it does not work. How do I do this? 回答1: You want your if check to be: {% if not

Call python function using HTML

强颜欢笑 提交于 2019-12-18 09:37:06
问题 I have a function in python that displays a list of names. def search(): with open('business_ten.json') as f: data=f.read() jsondata=json.loads(data) for row in jsondata['rows']: #print row['text'] a=str(row['name']) print a return a search() I am trying to call this function in an HTML file using Flask {% extends "layout.html" %} {% block content %} <div class="jumbo"> <h2>Welcome to the Rating app<h2> <h3>This is the home page for the Rating app<h3> </div> <body> <p>{{ search.a }}</p> <

Ansible concat vars to string

被刻印的时光 ゝ 提交于 2019-12-18 09:22:38
问题 I've spent most of the day trying to solve this problem and have thus far failed. I am building some playbooks to automate functions in Splunk, and am attempting to convert a list of hosts from an inventory group E.G. [search_head] 1.2.3.4 5.6.7.8 My expected (desired) result from the debug output of the play should be: https://1.2.3.4:8089, https://5.6.7.8:8089 I am attempting to complete this by running the following playbook against a running host: --- - name: Build search head list to

Flattening and filtering a complex structure in ansible - dict of list of dict

不想你离开。 提交于 2019-12-18 09:08:48
问题 I have data that is represented in this manner: { "key1": [{ "name": "some name1", "index": "some idx1" }, { "name": "some name2", "index": "some idx2" }, { "name": "some name3", "index": "some idx3" }], "key2": [{ "name": "some name4", "index": "some idx4" }, { "name": "some name5", "index": "some idx5" }, { "name": "some name6", "index": "some idx6" }] } I would like to convert the above to this, which is basically a dictionary with the existing key to a list of the indices. { "key1": [some

Is it possible to perform Includes with flask?

◇◆丶佛笑我妖孽 提交于 2019-12-18 05:28:05
问题 Say I have a template layout saved in template.html. This template includes a banner, side navigation, content container, and footer. Can I use flask to break up these page elements in such a way that I can have files such as banner.html, sidenavigation.html, etc. and render these different files within template.html? 回答1: From: http://jinja.pocoo.org/docs/templates/#include template.html {% include 'banner.html' %} {% include 'sidenavigation.html' %} {% include 'content.html' %} {% include

How do I get an Ansible template to honor new lines after a conditional

放肆的年华 提交于 2019-12-18 03:09:07
问题 The template looks like this: solr.replication.master= {% if ansible_eth0.ipv4.address == servermaster.eth0 %} false {% else %} true {% endif %} solr.replication.slave=false And the output should look like this: solr.replication.master=true solr.replication.slave=false What I am actually getting is: solr.replication.master=truesolr.replication.slave=false I understand that Jinja2 strips whitespace, and that ansible is probably configuring this by default. But it does not seem to honor -/+

Is there a way to hide the csrf label while looping through form using Flask and Flask-WTForms?

流过昼夜 提交于 2019-12-18 03:03:27
问题 I have very simple contact form and I would like to hide the label somehow so that it doesn't show Csrf Token . I am using Flask and Flask-WTForms and am rendering the form like this: {% for field in form %} {{ field.label }} {{ field }} {% endfor %} So basically this shows my inputs correctly and the csrf oen is hidden but the label isn't hidden? Should I get over it and implicitly say form.field_name instead of looping through the form or is there a way to handle this "corner case". I was

Python笔记(Jinja2)

南笙酒味 提交于 2019-12-18 02:42:33
本文是基于 Jinja2中文手册 ,以及 Jinja2 简明使用手册 所做的笔记。 1 Jinja2 Jinja2是基于python的模板引擎,之前在编写廖雪峰的web开发的时候接触过,在 Day5 以及 Day8 都有涉及到。 1.1 Environment 类 Environment 是Jinja2中的一个核心类,它的实例用来保存 配置 、 全局对象 ,以及 文件路径 用于加载模板。 配置Jinja2来为应用添加模板的步骤大致是: 创建 Environment 实例 使用 get_template() 加载模板 使用 render() 方法来渲染模板 1.1.1 创建 Environment 实例 现在结合廖雪峰的web开发实战一同说明,在Day5构建web框架的时候就有初始化Jinja2模板,这步骤就相当于创建 Environment 实例,创建 Environment 实例需要接受 主要 参数有(基本都有默认值): loader :模板加载器,形式是 loader=FileSystemLoader(path) ,其中的 path 表示的是模板的文件路径。 block_start_string :模块开始标记符,默认值 {% block_end_string :模块结束标记符,默认值 %} variable_start_string :变量开始标记符,默认值 {{