Using Jinja2 Templates in Django: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:Django Category:Web Development This is a useful link [http://jonathanchu.is/posts/upgrading-jinja2-templates-django-18-with-admin/ Upgrading to Jinja2 Templa...")
 
No edit summary
 
Line 19: Line 19:


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.
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: [http://jinja.pocoo.org/docs/dev/api/#high-level-api High level API] - Jinja2 documentation
To override the default value of a Jinja2 option:
<syntaxhighlight lang="python" highlight="12">
# settings.py
# [...]
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,
        }
    },
]

Latest revision as of 23:44, 4 December 2015

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[edit]

  • 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,
       }
   },

]