[Django] #32364: ModelAdmin.formfield_overrides drops TEMPLATES...DIRS while loading a custom widget template within a std admin form template

22 views
Skip to first unread message

Django

unread,
Jan 18, 2021, 10:53:17 PM1/18/21
to django-...@googlegroups.com
#32364: ModelAdmin.formfield_overrides drops TEMPLATES...DIRS while loading a
custom widget template within a std admin form template
-------------------------------------+-------------------------------------
Reporter: zoltan-ky | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Template | Version: 3.1
system |
Severity: Normal | Keywords: formfield_overrides
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Using Django 3.1.5. This custom widget "idea" is a minimal demo-version
based on
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides.

So I'd like to use a MyTextArea widget with its mytextarea.html on
models.TextField (myapp.Post.body) below and use it to edit Posts within
the admin. (I know how to do it through forms, creating my form fields
*and* my widgets, but that's a workaround).

When I 'runserver' and try to add a new Post under the admin a
"django.template.exceptions.TemplateDoesNotExist: myapp/mytextarea.html"
exception is thrown.

What am I missing in the test setup below?

I have traced the problem, as it is set up, (using pdb) to the EngineMixin
class (django/forms/renderer.py:39) which has a 'hard-coded' backend '
named 'djangoforms'. This DjangoTemplates instance used to instantiate
MyTextArea is **different** from the one used to find the admin form
template. Somewhere, and I don't understand enough of what's going on
here, the 'dirs' property of the first Engine (used for the form template)
gets dropped and not passed to the Engine used to load the custom widget
template.

To recreate (django 3.1.5): starting with a new: django-admin
startproject myproject; ... startapp myapp. After creating the few small
files below, migrate, createsuperuser for admin.

In the startproject-created *myproject/myproject/settings.py* the only
changes are:
* 'myapp' is added to INSTALLED_APPS
* TEMPLATES...DIRS: [ BASE_DIR / 'templates' ],
to use project-wide templates.

The files:
**myapp/admin.py**:
{{{
from django.contrib import admin
from django.db import models
from myapp.models import Post
from myapp.widgets import MyTextWidget
# Register your models here.
class PostAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': MyTextWidget}
}
admin.site.register(Post, PostAdmin)
}}}

**myapp/models.py**:
{{{
from django.db import models
# Create your models here.
class Post(models.Model):
body = models.TextField()
}}}

**myapp/widgets.py:**
{{{
from django import forms
from django.contrib.admin import widgets
class MyTextWidget(forms.Textarea):
template_name = 'myapp/mytextarea.html'
}}}

<project>**/templates/myapp/mytextarea.html**:
{{{
{# based on django/forms/templates/django/forms/widgets/textarea.html #}
<textarea name="{{ widget.name }}"{% include
"django/forms/widgets/attrs.html" %}>
<p>MY TEXTAREA</p>
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
}}}

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

Django

unread,
Jan 19, 2021, 12:36:50 AM1/19/21
to django-...@googlegroups.com
#32364: ModelAdmin.formfield_overrides drops TEMPLATES...DIRS while loading a
custom widget template within a std admin form template
-------------------------------------+-------------------------------------
Reporter: zoltan-ky | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: 3.1
Severity: Normal | Resolution: invalid
Keywords: formfield_overrides | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

See docs about
[https://docs.djangoproject.com/en/stable/ref/forms/renderers/#overriding-
built-in-widget-templates "Overriding built-in widget templates"]. You
should use `FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'`.

Please don't use Trac as a support channel. Closing per
TicketClosingReasons/UseSupportChannels.

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

Django

unread,
Jan 19, 2021, 9:28:58 AM1/19/21
to django-...@googlegroups.com
#32364: ModelAdmin.formfield_overrides drops TEMPLATES...DIRS while loading a
custom widget template within a std admin form template
-------------------------------------+-------------------------------------
Reporter: zoltan-ky | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: 3.1
Severity: Normal | Resolution:
| worksforme

Keywords: formfield_overrides | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by zoltan-ky):

* needs_docs: 0 => 1
* resolution: invalid => worksforme


Comment:

My apologies. I should have posted on the support channel first and will
do so in the future.

There is no mention of this need at
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
and is the example I followed

Maybe that part of the admin documentation should be updated to avoid the
trap.

--
Ticket URL: <https://code.djangoproject.com/ticket/32364#comment:2>

Reply all
Reply to author
Forward
0 new messages