#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070182bd26b13a-1b01f988-6108-41ef-89c8-e7af12d34c8b-000000%40eu-central-1.amazonses.com.