HTML Templates Using Jinja2 - Lost

I am trying to create an html template in python using Jinja2. I have a templates folder with my "template.html", but I don’t know how to handle environments or package loaders.

I installed Jinja2 using easy_python and ran the following script.

from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('yourapplication', 'templates')) template = env.get_template('mytemplate.html') print template.render() 

I get the following error because I don’t know how to determine the package / module. Please help me. I just want to create a simple template.

  File "log_manipulationLL.py", line 291, in <module> env = Environment(loader=PackageLoader('yourapplication', 'templates')) File "/usr/local/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg/jinja2/loaders.py", line 216, in __init__ provider = get_provider(package_name) File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 213, in get_provider __import__(moduleOrReq) ImportError: No module named yourapplication 
+7
source share
4 answers

PackageLoader expects a valid Python module using regular dot syntax. For example, if your structure looks like this:

 myapp/ __init__.py … templates/ mytemplate.html 

You should use myapp as the module name.

+8
source

If you do not want or do not need a Python package, most likely you should use FileSystemLoader , for example:

 from jinja2 import Environment, FileSystemLoader, select_autoescape env = Environment( loader=FileSystemLoader('file/path/'), autoescape=select_autoescape(['html', 'xml']), ) 
+9
source

I solved this problem using the following code:

  env = Environment(loader=PackageLoader('scriptname', templatesPath)) 

where this code is in the scriptname.py file.

I'm not sure my answer is appropriate, but I was wondering if someone might find this answer useful. If I am wrong, let me know.

+8
source

PackageLoader is defined as follows:

 class PackageLoader(BaseLoader): """Load templates from python eggs or packages. It is constructed with the name of the python package and the path to the templates in that package:: loader = PackageLoader('mypackage', 'views') If the package path is not given, ``'templates'`` is assumed. Per default the template encoding is ``'utf-8'`` which can be changed by setting the `encoding` parameter to something else. Due to the nature of eggs it only possible to reload templates if the package was loaded from the file system and not a zip file. """ 

And then the __init__() method looks like this:

 def __init__(self, package_name, package_path='templates', encoding='utf-8'): 

This makes us notice that such a structure is as follows:

 myapp/ __init__.py ... templates/ mytemplate.html 

Will have the same PackageLoader instance with both of these declarations:

 PackageLoader('myapp') PackageLoader('myapp', 'templates') 

So, if you use the myapp/ path, you just need to say:

 PackageLoader('templates', '') 

So it just takes templates/ as a path. If you leave the second argument empty, it will try to find the templates in templates/templates .

Finally, you can check what was loaded using the list_templates() method:

 PackageLoader('templates', '').list_templates() 
+1
source

All Articles