Skip to content

Commit 3c227d3

Browse files
wizpig64auvipy
authored andcommitted
Provide a better default template engine NAME than 'backend'
1 parent b18e701 commit 3c227d3

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

CHANGES.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ Unreleased
88
- Drop Python 3.6 and 3.7 support, now require >=3.8.
99
- Add Django 4.1 and 4.2 support.
1010
- Add Python 3.11 support.
11+
- Provide a better default template engine NAME than 'backend' (#303):
12+
13+
Previously, when configuring `TEMPLATES` in Django's settings,
14+
`NAME` had to be set to avoid the template engine's name becoming `"backend"`:
15+
16+
[source,python]
17+
----
18+
TEMPLATES = [
19+
{
20+
"NAME": "jinja2",
21+
"BACKEND": "django_jinja.backend.Jinja2",
22+
----
23+
24+
If your code matches that pattern, it can now be simplified to:
25+
26+
[source,python]
27+
----
28+
TEMPLATES = [
29+
{
30+
"BACKEND": "django_jinja.jinja2.Jinja2",
31+
----
32+
33+
There are no plans to remove support for the old `backend` import path, for consideration of existing projects.
34+
Also, be careful if you've set `NAME` to `"jinja"` (not `"jinja2"`)!
1135

1236

1337
Version 2.10.2

django_jinja/jinja2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
This import enables the import path of the django-jinja template backend
3+
to have a sane default NAME in your TEMPLATES setting.
4+
See: https://github.com/niwinz/django-jinja/pull/303
5+
"""
6+
7+
from .backend import Jinja2
8+
9+
__all__ = ["Jinja2"]

django_jinja/management/commands/makemessages.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ def add_arguments(self, parser):
6767

6868
def _get_default_jinja_template_engine(self):
6969
# dev's note: i would love to have this easy default: --jinja2-engine-name=jinja2
70-
# but as currently implemented, django-jinja's engine's name defaults to 'backend'.
70+
# but due to historical reasons, django-jinja's engine's name can default to either `jinja2` or 'backend'.
7171
# see: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-NAME
72-
# for now, the default engine will be the first one exactly matching our backend's path.
73-
return [e for e in engines.templates.values() if e["BACKEND"] == "django_jinja.backend.Jinja2"][0]
72+
# the default engine will be the first one exactly matching either of the new or old import paths.
73+
supported_import_paths = ["django_jinja.backend.Jinja2", "django_jinja.jinja2.Jinja2"]
74+
return [e for e in engines.templates.values() if e["BACKEND"] in supported_import_paths][0]
7475

7576
def handle(self, *args, **options):
7677
old_endblock_re = trans_real.endblock_re

doc/content.adoc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ Followed by the basic template engine configuration:
117117
----
118118
TEMPLATES = [
119119
{
120-
"NAME": "jinja2",
121-
"BACKEND": "django_jinja.backend.Jinja2",
120+
"BACKEND": "django_jinja.jinja2.Jinja2",
122121
"DIRS": [],
123122
"APP_DIRS": True,
124123
"OPTIONS": {}
@@ -139,12 +138,6 @@ django-jinja backend, take care of the template engines order, because the
139138
django-jinja backend by default uses the same directory for the templates as
140139
the django template engine. If you put the django engine first every jinja
141140
template will be found by the django engine.
142-
143-
Also, keep in mind that the automatically inferred `NAME` for the django-jinja
144-
backend will be `backend`. For this, you probably want to manually set the `NAME`
145-
setting to something more meaningful (e.g. `jinja2`), which you can later use
146-
when you need to specify a template engine by name
147-
(e.g. `render_to_string("myapp/template.jinja", context, using="jinja2")`).
148141
====
149142

150143
To read more on the logic of the `DIRS` and `APP_DIRS` settings,
@@ -338,6 +331,21 @@ from django_jinja.builtins import DEFAULT_EXTENSIONS
338331
339332
----
340333

334+
=== Setting the template engine name
335+
336+
Keep in mind that the automatically inferred `NAME` for any template backend
337+
link:https://docs.djangoproject.com/en/dev/ref/settings/#std-setting-TEMPLATES-NAME[will depend on the backend's import path].
338+
This name is used when you need to specify a template engine by name
339+
(e.g. `render_to_string("myapp/template.jinja", context, using="jinja2")`).
340+
341+
In previous versions of django-jinja, the backend's import path was `django_jinja.backend.Jinja2`,
342+
providing the unhelpful default engine name, `"backend"`.
343+
344+
Now, the import path `django_jinja.jinja2.Jinja2` can be used,
345+
which provides a more meaningful engine name, `"jinja2"`.
346+
347+
The old import path is still available for compatability with existing Django projects.
348+
341349

342350
=== Gettext Style
343351

@@ -363,8 +371,7 @@ This is a complete configuration example with django-jinja's defaults:
363371
----
364372
TEMPLATES = [
365373
{
366-
"NAME": "jinja2",
367-
"BACKEND": "django_jinja.backend.Jinja2",
374+
"BACKEND": "django_jinja.jinja2.Jinja2",
368375
"APP_DIRS": True,
369376
"OPTIONS": {
370377
"match_extension": ".jinja",

testing/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@
6161

6262
TEMPLATES = [
6363
{
64-
"BACKEND": "django_jinja.backend.Jinja2",
65-
"NAME": "jinja2",
64+
"BACKEND": "django_jinja.jinja2.Jinja2",
6665
"APP_DIRS": True,
6766
"OPTIONS": {
6867
"debug": True,

0 commit comments

Comments
 (0)