Opened 4 years ago

Last modified 9 months ago

#29115 assigned New feature

Allow rendering of admin forms with Jinja2

Reported by: Joey Wilhelm Owned by: David Smith
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Carlton Gibson, Ryan Hiebert Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

As previously posted to the django-developers mailing list:

According to the documentation[1], "...django.contrib.admin doesn’t include Jinja2 templates for its widgets due to their usage of Django template tags."

After some digging, though, it appears that the "spaceless" tag is the only one in use which is not readily available in Jinja2. And this is only used in django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html.

So, I was able to reimplement this template in Jinja2, minus the spaceless tag. On a few admin pages with large numbers of inlines, the load time was cut in half or better.

But, of course, there was one more small catch. I had to use the "TemplateSetting" form renderer rather than the "Jinja2" form renderer, because I had no way to alter the Jinja2 environment for the form renderer. The environment I refer to in my settings, however, is pulled almost verbatim from the documentation for the Jinja2 backend[2]. I only had to add one thing: i18n. Which turns out to be fairly simple to do with Jinja2 + Django. I was even able to use the "gettext" and "ngettext" from django.utils.translation.

So what I'm proposing here boils down to a few pieces:

  1. Create jinja2 templates for the admin widgets. I already have one of these done, and the others look like it may be possible to simply copy them.
  2. Document how to add i18n to the Jinja2 environment
  3. Perhaps provide a default environment to Jinja2, providing both static and url, as currently documented in the example Jinja2 environment, along with i18n. This would provide a better out-of-the-box experience for users, including being able to set the FORM_RENDERER to Jinja2, and have the admin Just Work.

Change History (8)

comment:1 Changed 4 years ago by Tim Graham

Description: modified (diff)
Summary: Rendering of admin forms with Jinja2Allow rendering of admin forms with Jinja2
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

comment:2 Changed 4 years ago by Joey Wilhelm

Owner: changed from nobody to Joey Wilhelm
Status: newassigned

comment:3 Changed 4 years ago by Joey Wilhelm

Has patch: set

comment:4 Changed 4 years ago by Carlton Gibson

Needs tests: set

comment:5 Changed 4 years ago by Carlton Gibson

Cc: Carlton Gibson added

comment:6 Changed 4 years ago by Ryan Hiebert

Cc: Ryan Hiebert added

comment:7 Changed 10 months ago by David Smith

When reviewing PR14819 it was noticed that django.forms.utils.flatatt() is now only used once by AdminReadonlyField.label_tag() and could be removed as part of the fix for this ticket, see comment

comment:8 Changed 9 months ago by David Smith

Owner: changed from Joey Wilhelm to David Smith
Note: See TracTickets for help on using tickets.
Back to Top