I know that Jinja2 vs DTL rendering can be a contentious issue at times, so
I want to start from the outset saying that I am not proposing
re-implementing the entire admin in Jinja2 templates!

However, that said, I believe that it is not only possible, but in fact
easy, to get forms to render using the Jinja2 engine in the admin. I've
actually just managed this on my current project at work, and I wanted to
see what thoughts would be for inclusion in core.

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, and I was off to the races. 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.

So, thoughts? Is this something worth filing a ticket for?

-Joey Wilhelm

[1]: https://docs.djangoproject.com/en/2.0/ref/forms/renderers/#jinja2
[2]:
https://docs.djangoproject.com/en/2.0/topics/templates/#django.template.backends.jinja2.Jinja2

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CADBkHdL%3Dx%2BKjjGhofmOecDtg%2B%2BNaCd14rsmqrG%2BN5r%3DLqE%2BSFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to