Using Jinja2 Templates in Django

From Littledamien Wiki
Revision as of 23:44, 4 December 2015 by Video8 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is a useful link Upgrading to Jinja2 Templates in Django 1.8 With Admin - jonathanchu.is

I managed to get this set up with the "northrose" project. Use that as reference.

|
+- jinja2/ << where I ultimately placed the jinja2 templates. It didn't work as documented under "templates"
|
+- mydjangoproject/
    |
    +- jinja.py << aliases for "url" and "static" defined here
    +- settings.py << jinja2 processor and environment set

TODO: document getting supporting url and static tags from Django templates.

Django convention: {% url "view-details" arg1 arg2 %}

Jinja2 convention: {{ url("view-details", args=[arg1, arg2, ]) }}, this is really an alias for Django's reverse() method, enclosed as a variable instead of as a tag.

Setting Jinja2 options

  • A list of options for Jinja2 templates: High level API - Jinja2 documentation

To override the default value of a Jinja2 option:

<syntaxhighlight lang="python" highlight="12">

  1. settings.py
  2. [...]

TEMPLATES = [

   # Jinja for front-end templates
   {
       'BACKEND': 'django.template.backends.jinja2.Jinja2',
       'DIRS': [os.path.join(BASE_DIR, 'jinja2'), ],
       'APP_DIRS': False,
       'OPTIONS': {
           'environment': 'northrose.jinja2.environment',
           'trim_blocks': True,
       }
   },

]