jinja2

Get variables from a settings.py file in a Jinja template with Flask

核能气质少年 提交于 2019-12-20 20:15:44
问题 Say I have settings.py file with a bunch of constants (maybe more, in the future). How do I access those variables in a Jinja template? 回答1: Flask automatically includes your application's config in the standard context. So if you used app.config.from_envvar or app.config.from_pyfile to pull in the values from your settings file, you already have access to those values in your Jinja templates (e.g., {{ config.someconst }} ). 回答2: You need to define a context_processor : @app.context_processor

How to csrf_token protection in jinja2 template engine?

故事扮演 提交于 2019-12-20 16:30:12
问题 In Django template I used: <form action="/user" method="post">{% csrf_token %} {{ form.as_p|safe }} <input type="submit" value="Submit" /> </form> But error when I change to jinja2 template engine : Encountered unknown tag 'csrf_token' My question: csrf_token protection in jinja2 is required? If required, how to do this? Thanks in advance! 回答1: It seems Jinja2 works differently: Use <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"> where in Django templates you use {%

Jinja keep indentation on include or macro

爷,独闯天下 提交于 2019-12-20 11:52:52
问题 I am wondering if there is any way to keep the indentation with jinja when adding a include or macro inside a file. I would like to use jinja to generating a code file. An example would be File: class.html class MyClass: def someOp(): pass {% include "someOp.html" %} File: someOp.html def someOp2(): pass The result of the template should be: class MyClass: def someOp(): pass def someOp2(): pass If there any way to make jinja prepend the indent before the include tag for each line in the

Jinja2 template not rendering if-elif-else statement properly

风格不统一 提交于 2019-12-20 10:59:56
问题 I am trying to set the text color using css in a jinja2 template. In the following code I want to set the output string to print in a specific font color if the variable contains a string. Everytime the template is generated though it prints in red due to the else statement, it never see the first two conditions even though the output should be matched, I can tell what the output from the variable is when the table generates and it is as expected. I know my css is correct due to the printing

Ansible write variables into YAML file

穿精又带淫゛_ 提交于 2019-12-20 10:55:55
问题 I have a specific ansible variable structure that I want to get from the vault into a yaml file on my hosts. Lets assume a structure like this: secrets: psp1: username: this password: that secret_key: 123 ... I need something like a "generic" template to output whatever "secrets" contains at the moment, since the content changes almost completely based on the current environment. The easiest solution I can think of is to output the whole structure in an template like this: # config/secrets

jinja2 how to remove trailing newline

时间秒杀一切 提交于 2019-12-20 09:48:16
问题 I'm using jinja 2 to output a yaml file but can't seem to get rid of a trailing newline and the end of a for loop. Eg the below - request: path: {{ path }} headers: origin: 'somedomain.com' user-agent: 'agent' referer: 'some.domain.com' authority: 'somedomain.com' querystring: {% for key, value in querystring.items() -%} {{ key }}: '{{ value }}' {% endfor %} response: content: file: {{ content }} gives me the output: - request: path: /some/path headers: origin: 'somedomain.com' user-agent:

Getting a request parameter in Jinja2

泄露秘密 提交于 2019-12-20 09:10:05
问题 How can I retrieve a request param a in Jinja2 template? http://foo.bar?a=1 回答1: I'm a bit late with this answer, but the other solutions don't really account for your use of Flask. The fact that you're using Flask with Jinja2 makes your situation a bit different from other frameworks. Flask actually makes some global variables available to you in all Jinja2 templates without requiring you to pass them to the template explicitly. To quote a part of the Flask documentation at http://flask

Flask Dynamic data update without reload page

a 夏天 提交于 2019-12-20 08:24:06
问题 i'm trying to create something like Google Suggest Tool (via suggest api http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q=query ) I'm listening input changes, and send data go GET: $("#search_form_input").keyup(function(){ var some_var = $(this).val(); $.ajax({ url: "", type: "get", //send it through get method data:{jsdata: some_var}, success: function(response) { }, error: function(xhr) { //Do Something to handle error } }); After that i'm handling this data and send

Flask Dynamic data update without reload page

你离开我真会死。 提交于 2019-12-20 08:23:03
问题 i'm trying to create something like Google Suggest Tool (via suggest api http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q=query ) I'm listening input changes, and send data go GET: $("#search_form_input").keyup(function(){ var some_var = $(this).val(); $.ajax({ url: "", type: "get", //send it through get method data:{jsdata: some_var}, success: function(response) { }, error: function(xhr) { //Do Something to handle error } }); After that i'm handling this data and send

create permenant unique links based on a user ID [duplicate]

混江龙づ霸主 提交于 2019-12-20 07:38:03
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: create unique profile page for each user python I am using google appengine with python and jinja2 and trying to give each user in my app a unique URL to their profile page that can be visited by anyone without logging in. Here is my code thus far: class ProfilePage(webapp2.RequestHandler): def get(self, profile_id): user = User.get_by_id(profile_id) #profile_id = some unique field if user: #Get all posts for