[Django] #33943: Add django.template.Engine.get_default_backend for easier access to backend methods

7 views
Skip to first unread message

Django

unread,
Aug 20, 2022, 5:26:50 PM8/20/22
to django-...@googlegroups.com
#33943: Add django.template.Engine.get_default_backend for easier access to backend
methods
-------------------------------------------+--------------------------
Reporter: Peter Thomassen | Owner: nobody
Type: New feature | Status: assigned
Component: Template system | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+--------------------------
Assume that the task is to create a template from string using
DjangoTemplates.from_string() of the default template backend.

(This has the special feature that the result of Engine.from_string() is
wrapped to construct an instance of the Template class defined in the
backend definition file: django/template/backends/django.py#30 This
Template class differs from the Engine-delivered Template in that it
accepts a dictionary as the context, see
https://docs.djangoproject.com/en/4.1/topics/templates/#django.template.backends.base.Template.render.)

The problem is that it is difficult to retrieve the default template
backend in order to call .from_string() on it. However, a function already
exists to retrieve the default template backend's *engine*.

The default backend's engine is retrieved via
django.template.Engine.get_default(). This function identifies the first
configured DjangoTemplates backend, and then returns its .engine
attribute. As a result, when from_string() is called, it will be called on
the engine and not on the backend, thus the wrapping Template() call is
missing.

I am proposing to add a get_default_backend() function to
django.template.Engine, which is like get_default(), but returns the
backend itself (and not its .engine attribute). This would allow calling
.from_string() directly on the backend subsequently. -- get_default() can
then be adjusted to simply return get_default_backend().engine.

While trying to solve the issue (context argument type mismatch: dict vs
Context), I found the docs not very helpful, as they are not very accurate
in the distinction between a backend and its engine. I've also added a
commit that clarifies that language.

I have submitted a patch here: https://github.com/django/django/pull/15944
For discussion on the list, see here: https://groups.google.com/g/django-
developers/c/BfB9QsDMMAI

--
Ticket URL: <https://code.djangoproject.com/ticket/33943>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 23, 2022, 3:27:36 AM8/23/22
to django-...@googlegroups.com
#33943: Add django.template.Engine.get_default_backend for easier access to backend
methods
---------------------------------+--------------------------------------

Reporter: Peter Thomassen | Owner: nobody
Type: New feature | Status: closed

Component: Template system | Version: 4.1
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: assigned => closed
* resolution: => needsinfo


Comment:

I'll close as needsinfo pending the mailing list discussion. (We can
reopen if we decide to progress.) Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/33943#comment:1>

Reply all
Reply to author
Forward
0 new messages