Using utf-8 characters in a Jinja2 template

TL;DR: Pass Unicode to template.render() Encode the rendered unicode result to a bytestring before writing it to a file This had me puzzled for a while. Because you do index_file.write( template.render(index_variables) ) in one statement, that’s basically just one line where Python is concerned, so the traceback you get is misleading: The exception I got … Read more

Concatenate lists in JINJA2

AFAIK you can’t do it using native Jinja2 templating. You’re better off creating a new combined iterable and passing that to your template, eg: from itertools import chain x = xrange(3) y = xrange(3, 7) z = chain(x, y) # pass this to your template for i in z: print i As per comments, you … Read more

How can I send variables to Jinja template from a Flask decorator?

I’m going to propose something even simpler than using a decorator or template method or anything like that: def render_sidebar_template(tmpl_name, **kwargs): (var1, var2, var3) = generate_sidebar_data() return render_template(tmpl_name, var1=var1, var2=var2, var3=var3, **kwargs) Yup, just a function. That’s all you really need, isn’t it? See this Flask Snippet for inspiration. It’s essentially doing exactly the same … Read more

Does the Jinja2 templating language have the concept of ‘here’ (current directory)?

I do not believe so. Typically you include or extend other templates by specifying their paths relative to the root of whatever template loader and environment you’re using. So let’s say your templates are all in /path/to/templates and you’ve set up Jinja like so: import jinja2 template_dir=”/path/to/templates” loader = jinja2.FileSystemLoader(template_dir) environment = jinja2.Environment(loader=loader) Now, if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)