jinja2

Flask-sqlalchemy column alias for browser output

拥有回忆 提交于 2019-12-13 00:55:06
问题 I'm using a for loop to output the columns and values of a single database row. This is all working but there are a couple of issues. The column names aren't suitable to output in the browser so I'm looking for a way to associate an alias (not sure this is the correct term) eg. column names: cust_name cust_area Desired output: Customer name Customer area models.py class Customers(db.Model): id = db.Column(db.Integer, primary_key = True) cust_name = db.Column(db.String(64)) cust_area = db

Compile subsection of Jinja2 AST

自古美人都是妖i 提交于 2019-12-12 21:05:16
问题 Can one compile or revert a portion of the Jinja2 AST? For example, is it possible to call a function or method from jinja2.environment or jinja2.compiler.generate or some equivalent on a list of nodes extracted from within a template? For example, given a template y.html : avant-tag {% xyz %} tag content {{ 3 + 5 }} {% endxyz %} apres-tag and an extension y.py : # -*- coding: utf-8 -*- from jinja2 import nodes, Environment, FileSystemLoader from jinja2.ext import Extension class YExtension

Jinja2 macro import “with context” and global variables: {% from file.html import macro_name with context %}

走远了吗. 提交于 2019-12-12 18:43:16
问题 In Jinja2 template engine for python, my macro "with context" could not access the global variables from the context. Import statements: {% import 'en-US/my_macro.html' as all_macros %} {% from 'en-US/my_macro.html' import the_macro with context %} Macro usage: {{ all_macros.the_macro() }} The macro is being called, but the globals have no value. Here's the relevant docs for "with context" 回答1: The issue arises because there are 2 ways to call the macro. One is from the all_macros import. The

After installing jinja2 TemplateDoesNotExist at /admin/

狂风中的少年 提交于 2019-12-12 17:01:19
问题 I have installed jinja2 and after that 'DIRS' stopped working(I have to include them by hand). Changing 'APP_DIRS' doesn`t help templates look like that: TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'APP_DIRS': False, 'DIRS': ['main/templates', 'shop/templates'], 'OPTIONS': { 'environment': 'django_test.create_jinjia_env.environment', 'autoescape': True, 'auto_reload': DEBUG, 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context

ImportError: cannot import name is_python_keyword

我们两清 提交于 2019-12-12 16:57:49
问题 I am trying to execute a python script , but I get an error on line from jinja2.utils import Markup, concat, escape, is_python_keyword, next ImportError: cannot import name is_python_keyword I checked there is no file named is_python.py 回答1: Looking at the source code for 2.3.1 they have a line: from keyword import iskeyword as is_python_keyword They are using the builtin keyword module. The current version is 2.7.3 so it seems they have changed the code and it is no longer available. You

Jinja2 filter to convert custom markup to html

拜拜、爱过 提交于 2019-12-12 16:01:36
问题 Having the autoescape property on (I want to keep it that way), I want user to be able to enter some custom markup, to have the opportunity to format text. For example, [s][/s] will be translated into <strong></strong> . I believe the right way to do this is to write the custom Jinja2 filter. But the following doesn't work: @app.template_filter() @evalcontextfilter def mark2html(eval_ctx, value): result = escape(value).replace('[s]','<strong>') if eval_ctx.autoescape: result = Markup(result)

Applying a jinja filter after using groupby filter

陌路散爱 提交于 2019-12-12 15:23:01
问题 I have a list of dictionaries that I want to group by a certain attribute and then sum by another. For a variable 'foo' this would be something like: foo | groupby('a') | sum(attribute='b') This clearly won't work because after the groupby , I have a list of tuples. Is there any way to unpack the tuple and then repack it, so that i can maintain the work done by groupby , but work on the second tuple value? 回答1: You can use the map() filter to apply a sum to each group, provided you extract

jinja2 template not found and internal server error

守給你的承諾、 提交于 2019-12-12 14:34:11
问题 Python code: from flask import Flask, render_template app = Flask(__name__) @app.route("/") def hello(): return render_template('testing.html') if __name__ == "__main__": app.run() Html file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>My name is pk</h1> </body> </html> Also how to enable jinja2 in pycharm community version. I am directly creating html file and .py file in the same project 回答1: flask file structure |-app |--templates

Jinja, get line of source code after template rendering

社会主义新天地 提交于 2019-12-12 12:32:37
问题 Is it possible to get line of source code for each line of rendered template? For example if we have template like below: some_expr {% if true %} other_expr {% endif %} So, after template rendering following text is produced: some_expr other_expr I want to get information that 'some_expr' is produced from 1st line of template source code, and 'other_expr' if produced from 3rd line. Even better if I can get file name and line from which rendered line is produced (in case of including other

Bootstrap accordion panel-body not collapsing on first hit

岁酱吖の 提交于 2019-12-12 11:22:08
问题 I have the following accordion defined which otherwise works fine, except that the first panel-body does not collapse after expanding the second panel-body, though it does eventually collapse if I click the Second headline for a second time. I can work around the problem by using "panel-collapse collapse" instead of "panel-collapse collapse in" for the first headline, however I would like to avoid having to do this as this reduces the usability of the page. Any advise of what may be causing