• Stars
    star
    178
  • Rank 214,989 (Top 5 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 15 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An adapter for using Jinja2 templates with Django.
.. module:: jingo

Jingo

Jingo is an adapter for using Jinja2 templates within Django.

Jingo is DEPRECATED

In version 1.8, Django added support for multiple template engines, and provided a Jinja2 backend. The django-jinja project leverages that to support Jinja2, while Jingo does not.

django-jinja is recommended for new projects. Jingo >=0.8 supports Django 1.8, but it will not be maintained beyond version 0.9, and will not support Django 1.9 or above. If you're already using Jingo, and not ready to make the switch, Jingo should continue to work for now, though not without some effort.

0.9 will be the last release of Jingo, unless a new maintainer comes along with a new direction.

As of 0.9, Jingo's built-in helpers are provided via a Jinja2 extension to simplify moving away from Jingo. The entire jingo/ext.py file can be copied into another project, or referenced as 'jingo.ext.JingoExtension'. Used in this way, Jingo plays nicely with django-jinja (and theoretically Django's built-in Jinja2 backend).

Usage

When configured properly (see Settings below) you can render Jinja2 templates in your view the same way you'd render Django templates:

from django.shortcuts import render


def my_view(request):
    context = dict(user_ids=(1, 2, 3, 4))
    return render(request, 'users/search.html', context)

Note

Not only does django.shorcuts.render work, but so does any method that Django provides to render templates.

Settings

You'll want to use Django to use jingo's template loader. In settings.py:

TEMPLATE_LOADERS = (
    'jingo.Loader',
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

This will let you use django.shortcuts.render or django.shortcuts.render_to_response.

You can optionally specify which filename patterns to consider Jinja2 templates:

JINGO_INCLUDE_PATTERN = r'\.jinja2'  # use any regular expression here

This will consider every template file that contains the substring .jinja2 to be a Jinja2 file (unless it's in a module explicitly excluded, see below).

And finally you may have apps that do not use Jinja2, these must be excluded from the loader:

JINGO_EXCLUDE_APPS = ('debug_toolbar',)

If a template path begins with debug_toolbar, the Jinja loader will raise a TemplateDoesNotExist exception. This causes Django to move onto the next loader in TEMPLATE_LOADERS to find a template - in this case, django.template.loaders.filesystem.Loader.

Note

Technically, we're looking at the template path, not the app. Often these are the same, but in some cases, like 'registration' in the default setting--which is an admin template--they are not.

The default is in jingo.EXCLUDE_APPS:

EXCLUDE_APPS = (
    'admin',
    'admindocs',
    'registration',
    'context_processors',
)
.. versionchanged:: 0.6.2
   Added ``context_processors`` application.

If you want to configure the Jinja environment, use JINJA_CONFIG in settings.py. It can be a dict or a function that returns a dict.

JINJA_CONFIG = {'autoescape': False}

or:

def JINJA_CONFIG():
    return {'the_answer': 41 + 1}

If you set the extensions key in the configuration, you must include jingo.ext.JingoExtension to get Jingo's built-in template helpers (see below).

Template Helpers

Note

In the interest of future-proofing, consider writing custom filters and functions as Jinja extensions. See jingo/ext.py for a simple example.

Instead of template tags, Jinja encourages you to add functions and filters to the templating environment. In jingo, we call these helpers. When the Jinja environment is initialized, jingo will try to open a helpers.py file from every app in INSTALLED_APPS. Two decorators are provided to ease the environment extension:

.. function:: jingo.register.filter

    Adds the decorated function to Jinja's filter library.

.. function:: jingo.register.function

    Adds the decorated function to Jinja's global namespace.


Default Helpers

Helpers are available in all templates automatically, without any extra loading. See jingo/ext.py for their definitions.

Template Environment

A single Jinja Environment is created for use in all templates. This is available via jingo.get_env() if you need to work with the Environment.

Localization

Since we all love L10n, let's see what it looks like in Jinja templates:

<h2>{{ _('Reviews for {0}')|f(addon.name) }}</h2>

The simple way is to use the familiar underscore and string within a {{ }} moustache block. f is an interpolation filter documented below. Sphinx could create a link if I knew how to do that.

The other method uses Jinja's trans tag:

{% trans user=review.user|user_link, date=review.created|datetime %}
    by {{ user }} on {{ date }}
{% endtrans %}

trans is nice when you have a lot of text or want to inject some variables directly. Both methods are useful, pick the one that makes you happy.

Forms

Django marks its form HTML "safe" according to its own rules, which Jinja2 does not recognize.

This monkeypatches Django to support the __html__ protocol used in Jinja2 templates. Form, BoundField, ErrorList, and other form objects that render HTML through their __unicode__ method are extended with __html__ so they can be rendered in Jinja2 templates without adding |safe.

Call the patch() function to execute the patch. It must be called before django.forms is imported for the conditional_escape patch to work properly. The root URLconf is the recommended location for calling patch().

Usage:

import jingo.monkey
jingo.monkey.patch()

Testing

To run the test suite, you need to define DJANGO_SETTINGS_MODULE first:

$ export DJANGO_SETTINGS_MODULE="fake_settings"
$ nosetests

or simply run:

$ python run_tests.py

To test on all supported versions of Python and Django:

$ pip install tox
$ tox

More Repositories

1

django-multidb-router

Round-robin master/slave db router for Django.
Python
313
star
2

glow

(glow)$ gci -m 'glow is an application for counting firefox downloads'
Python
78
star
3

django-mobility

Middleware and decorators for directing users to your mobile site.
Python
60
star
4

check

Simple script to run pep8.py and pyflakes on a git/svn repo.
Python
42
star
5

tornado-websocket-client

Python
40
star
6

django-debug-cache-panel

A sweet little cache panel for django-debug-toolbar
Python
39
star
7

test-utils

A grab-bag of testing utilities that are pretty specific to our Django, Jinja2, and nose setup.
Python
33
star
8

push

Push notifications for the web
Python
30
star
9

schematic

The worst schema versioning system, ever?
Python
26
star
10

dotfiles

shell customizations gone awry
Vim Script
21
star
11

ribbons

Remaking the github ribbons in CSS
21
star
12

python-irclib

Fork of http://sourceforge.net/projects/python-irclib/, mainly so I can install it with pip.
Python
19
star
13

github-notifications

Push notifications for github
JavaScript
17
star
14

chief

development has moved to mozilla/chief
Python
16
star
15

vending-machine

a script for managing vendor libraries
Python
14
star
16

path.py

path.py provides a class (path) for working with files and directories. Less typing than os.path, more fun, a few new tricks.
12
star
17

reflecto

A tiny wsgi app to mirror github repos on a commit hook.
Python
9
star
18

push-addon

Firefox add-on implementing the push notfications API
JavaScript
9
star
19

git-tools

Scripts that make my git workflow smooth and tasty.
Python
9
star
20

bookmark-shortcut-keys

Firefox add-on that lets you access Bookmark Toolbar items with a keyboard shortcut
JavaScript
8
star
21

jetpacks

Features I have created for great good.
JavaScript
5
star
22

bosley

a british buildbot/ircbot for running AMO tests
Python
4
star
23

freddo

oremj's long-lost brother
Python
4
star
24

bots

hacky chatbots I threw together
JavaScript
3
star
25

moonshoes

moonshoes is a redirection service that will make your urls a lot longer.
Python
3
star
26

poboy

Extracts gettext fallbacks to messages.po, useful on AMO
Python
2
star
27

reredis

proxy redis connections so it's not a single point of failure
JavaScript
2
star
28

zurg

surely the world needs more static site generators
Python
2
star
29

pusher

push consumer
Python
2
star
30

freddo-lib

vendor lib for freddo
Python
2
star
31

bzattach

Uploading bugzilla attachments from the command line is the future!
Python
2
star
32

jbalogh.github.io

A website, on the Internet
CSS
2
star
33

bztools

Models and scripts to access the Bugzilla REST API.
2
star
34

modconcat

PHP helper functions for using mod_concat.
PHP
1
star
35

twttr

A twitter clone for a workshop in scalability. The poor performance is intentional.
Python
1
star