jinja2

Text escaped when I want it to show up as html in Flask/jinja2

烂漫一生 提交于 2019-12-30 01:39:06
问题 I pull a feed from rss and store the data in a appengine db. The rss feed content includes the entire html. So I have this python code: @app.route("/rssRead") def pullRss(): feedItem = db.getFeedItemByName(request.args.get('title',None), request.args.get('key',None)) return render_template("rss.html", data= Markup(feedItem.html).unescape()) And my html template looks like this: {% extends "layout.html" %} {% block body %} {{ data }} {% endblock %} So when I view the page I have the actual

二十:jinja2之加载静态文件

為{幸葍}努か 提交于 2019-12-29 11:31:26
静态文件: flask默认指定的静态文件路径为根目录下的static,可以自定义路径,并指定,使用url_for('文件夹', filename='文件名')引用 加载css文件 加载js文件 其他文件加载方式一样,只不过需要根据具体文件类型使用不同的标签 来源: https://www.cnblogs.com/zhongyehai/p/11784648.html

Jinja2 templates using Django template tags

爷,独闯天下 提交于 2019-12-29 08:50:53
问题 I'm using Jinja2 on a new project, but would like to use the django-socialregistration app, which relies on Django template tags. Jinja2 doesn't play nicely with template tags, so I'm wondering if there's a quick workaround? Template tags: {% load facebook_tags %} {% facebook_button %} {% facebook_js %} This previous question addresses the same topic for Mako, but I'm having trouble adapting it to work with Jinja2. The following is my attempt at an adaptation (non-working): {% from django

Generate random number with jinja2

柔情痞子 提交于 2019-12-29 08:27:08
问题 I need to generate a random number between 1 and 50. Aparently the random filter needs a sequence. How can I create a list with numbers from 1 to 50 in Jinja? {{ [1,n,50]|random() }} 回答1: Jinja2 also includes the range function which returns a sequence of numbers from start to end - 1 , so you can use it with random: Your lucky number is: {{ range(1, 51) | random }} 来源: https://stackoverflow.com/questions/36683951/generate-random-number-with-jinja2

Hack Jinja2 to encode from `utf-8` instead of `ascii`?

≡放荡痞女 提交于 2019-12-29 08:18:23
问题 Jinja2 converts all template variables into unicode before processing. Can anybody find a place where does this happen? The problem is that it assumes that strings are ascii , but we (at Roundup) are using utf-8 internally and our ORM (HyperDB) restores object properties to utf-8 automatically, and converting them all into unicode in every view just before passing to templates is too much legwork. 回答1: Answer from Armin: Unfortunately that is impossible. Jinja uses the default string coercion

using regex in jinja 2 for ansible playbooks

大兔子大兔子 提交于 2019-12-29 05:56:32
问题 HI i am new to jinja2 and trying to use regular expression as shown below {% if ansible_hostname == 'uat' %} {% set server = 'thinkingmonster.com' %} {% else %} {% set server = 'define yourself' %} {% endif %} {% if {{ server }} match('*thinking*') %} {% set ssl_certificate = 'akash' %} {% elif {{ server }} match( '*sleeping*')%} {% set ssl_certificate = 'akashthakur' %} {% endif %} based on the value of "server" i would like to evaluate as which certificates to use. ie if domain contains

Import a Python module into a Jinja template?

北战南征 提交于 2019-12-29 02:54:33
问题 Is it possible to import a Python module into a Jinja template so I can use its functions? For example, I have a format.py file that contains methods for formatting dates and times. In a Jinja macro, can I do something like the following? {% from 'dates/format.py' import timesince %} {% macro time(mytime) %} <a title="{{ mytime }}">{{ timesince(mytime) }}</a> {% endmacro %} Because format.py is not a template, the code above gives me this error: UndefinedError: the template 'dates/format.py'

Create dynamic arguments for url_for in Flask

我与影子孤独终老i 提交于 2019-12-27 12:26:50
问题 I have a jinja2 template which I reuse for different Flask routes. All of these routes have a single required parameter and handle only GET requests, but some routes may have extra arguments. Is there a way to append extra arguments onto url_for() ? Something like url_for(my_custom_url, oid=oid, args=extra_args) which will render to (depending on the route endpoint): # route 'doit/<oid>' with arguments doit/123?name=bob&age=45 # route 'other/<oid>' without arguments other/123 My use case

Can a value in a variable and a string with backslashes be combined in ansible?

你说的曾经没有我的故事 提交于 2019-12-25 09:09:00
问题 I have an ansible playbook in which one variable I am passing from the command. I am trying to append a windows folder path to it. One way I am able to find out is to add the path to another variable and then join the two variable. I would like to know if it is possible to avoid the variable and put the path like this: "{{ variable2 }} \build\dist\package\ui.msi" variable1 has value "d:\install" var_build_file_name is entered by the user variable2 is formed by combining variable1 and var

Webapp2 - TypeError: get() takes exactly 1 argument (2 given)

狂风中的少年 提交于 2019-12-25 09:06:05
问题 I have a /consults page displaying a list of consults. My list loop looks like this: {% for consult in consults %} <tr> <td><a href="/consults/view-consult?key={{consult.key.urlsafe()}}">{{ consult.consult_date }}</a></td> <td>{{ consult.consult_time }}</td> <td>{{ consult.patient_first }}</td> <td>{{ consult.patient_last }}</td> <td><span class="badge badge-warning">{{ consult.consult_status }}</span></td> </tr> {%endfor%} So I'm using the url to send a Consult key to the individual page to