jinja2

render jinja2 template without a Flask context

烈酒焚心 提交于 2020-01-10 19:26:06
问题 I have a Flask application that calls flask.render_template without problems when it is invoked from a flask http request . I need the same method to work outside of flask (from a python back end program) resolved_template = render_template(template_relative_path, **kwargs) I could use the jinja2 api , but I would like the same method to work, in both contexts (flask and command line) 回答1: You need to render it in an app context. Import your app in your backend code and do the following. with

Ansible template adds 'u' to array in template

不打扰是莪最后的温柔 提交于 2020-01-10 14:09:47
问题 I have the following vars inside of my ansible playbook I got the following structure domains: - { main: 'local1.com', sans: ['test.local1.com', 'test2.local.com'] } - { main: 'local3.com' } - { main: 'local4.com' } And have the following inside of the my conf.j2 {% for domain in domains %} [[acme.domains]] {% for key, value in domain.iteritems() %} {% if value is string %} {{ key }} = "{{ value }}" {% else %} {{ key }} = {{ value }} {% endif %} {% endfor %} {% endfor %} Now when I go in the

Flask application built using pyinstaller not rendering index.html

半城伤御伤魂 提交于 2020-01-10 08:23:07
问题 I have written a flask application and it works perfectly fine. I wanted to distribute it as an executable. Tried doing it using pyinstaller flaskScript.py dist folder got generated. Went into the dist folder and double clicked my executable flaskScript, it starts my server. On accessing the url, localhost:9090 it gives the following exception jinja2.exceptions.TemplateNotFound TemplateNotFound: index.html Traceback (most recent call last) File "/Users/george/Downloads/flaskApps/flaskScript

Get lengths of a list in a jinja2 template

倖福魔咒の 提交于 2020-01-09 02:53:37
问题 How do I get the number of elements in a list in jinja2 template? For example, in Python: print(template.render(products=[???])) and in jinja2 <span>You have {{what goes here?}} products</span> 回答1: <span>You have {{products|length}} products</span> You can also use this syntax in expressions like {% if products|length > 1 %} jinja2's builtin filters are documented here; and specifically, as you've already found, length (and its synonym count ) is documented to: Return the number of items of

Python web开发:几个模板系统的性能对比

和自甴很熟 提交于 2020-01-08 18:28:21
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 对比目标,jinja2,cheetah,mako,webpy,bottle,tornado,django的性能。 方法,随机生成一个二维数组,第一列是自增数据,第二列是长度为100的随机字符串,然后生成html,比较一次生成的时间。 说明,如果模板有编译缓存,打开。有其他方法加速,打开。生成缓存,关闭。不计算随机数据生成时间,一次生成后一直使用。 以下是文件有效内容,没用的都略去了。最后的顺序是因为我根据结果整理了一下调用次序。 —–testcheetah.tmpl—– #for $i in $l #end for $i[0] $i[1] —–testdjango.html—– {% for i in l %} {% endfor %} {{ i.0 }} {{ i.1 }} —–testjinja2.html—– {% for i in l %} {% endfor %} {{ i[0] }} {{ i[1] }} —–testmako.html—– % for i in l: % endfor ${i[0]} ${i[1]} —–testwebpy.html—– $def with(l) $for i in l: $i[0] $i[1] —–tmpl.py—– #!/usr/bin/python # -﹡-

how to start the forloop.counter from a different index

笑着哭i 提交于 2020-01-07 09:49:21
问题 I have 2 seperate forloops and i am using forloop.counter in bothloops. I want to start the second forloop counter from the ending of first forloop {% for i in something1 %} <tr> <td>{{ forloop.counter }}</td> <td>i.username</td> </tr> {% endfor %} {% for j in something2 %} <tr> <td>{{ forloop.counter }}</td> <td>j.username</td> </tr> {% endfor %} if the first forloop ends at 10 then i want to start the next for loop from 11.plz help 回答1: I'm not comfortable with Django, so I show a couple of

Change variable in Ansible template based on group with children?

时光总嘲笑我的痴心妄想 提交于 2020-01-07 05:23:48
问题 I found Change variable in Ansible template based on group. However how do I extend the answer in groups with children? If I extend the group vars file in the link above to [host0] host0.my-network.com [host1] host1.my-network.com [es-masters:children] host0 host1 How can I extend the above post's answer (shown below) to make it work with the group vars file above? Simply putting the FQDN in the jinja2 statement does not work. {% if ansible_fqdn in groups['es-masters'] %} node_master=true {%

Change variable in Ansible template based on group with children?

纵然是瞬间 提交于 2020-01-07 05:23:04
问题 I found Change variable in Ansible template based on group. However how do I extend the answer in groups with children? If I extend the group vars file in the link above to [host0] host0.my-network.com [host1] host1.my-network.com [es-masters:children] host0 host1 How can I extend the above post's answer (shown below) to make it work with the group vars file above? Simply putting the FQDN in the jinja2 statement does not work. {% if ansible_fqdn in groups['es-masters'] %} node_master=true {%

How to make a list of pairs unique in Jinja?

主宰稳场 提交于 2020-01-07 01:22:09
问题 I have a dictionary with array values coming from an Ansible vars file. ipfilter: zone1: - { application: "app 1", local_ip: 1.1.1.1 } - { application: "app 1", local_ip: 1.1.1.1 } - { application: "app 2", local_ip: 2.2.2.2 } zone2: - { application: "app 3", local_ip: 3.3.3.3 } - { application: "app 4", local_ip: 4.4.4.4 } - { application: "app 4", local_ip: 4.4.4.4 } The problem is, that the tuple of application and local_ip is not unique, because the objects contain additional data, which

How to update User with webapp2 and simpleauth?

﹥>﹥吖頭↗ 提交于 2020-01-06 15:18:35
问题 I have a profile page for my users where they should be able to update their information. For now they can update their names but I also want phonenumbers, addresses, etc. The code for updating the name of my user is class AccountPage(BaseRequestHandler): def get(self): self.render('accountpage.html', {'request': self.request, 'user': self.current_user,'loggedin': self.logged_in, 'session': self.auth.get_user_by_session(),}) def post(self): user = self.current_user user.name = self.request