Jinja2 load the template from the line: TypeError: no loader for the specified environment

I am using Jinja2 in Flask. I want to display a template from a string. I tried the following 2 methods:

rtemplate = jinja2.Environment().from_string(myString) data = rtemplate.render(**data) 

and

  rtemplate = jinja2.Template(myString) data = rtemplate.render(**data) 

However, both methods return:

 TypeError: no loader for this environment specified 

I checked the manual and this url: https://gist.github.com/wrunk/1317933

However, nowhere is the bootloader selection indicated when using the string.

+6
source share
1 answer

You can provide loader in Environment from this list

 from jinja2 import Environment, BaseLoader rtemplate = Environment(loader=BaseLoader).from_string(myString) data = rtemplate.render(**data) 

Change The problem was in myString , it has {% include 'test.html' %} , and Jinja2 has no idea where to get the template from.

UPDATE

As @ iver56 noted, better:

 rtemplate = Environment(loader=BaseLoader()).from_string(myString) 
+12
source

All Articles