#33943 closed New feature (needsinfo)

Add django.template.Engine.get_default_backend for easier access to backend methods

Reported by: Peter Thomassen Owned by: nobody
Component: Template system Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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

Change History (1)

comment:1 Changed 16 months ago by Carlton Gibson

Resolution: needsinfo
Status: assignedclosed

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

Note: See TracTickets for help on using tickets.
Back to Top