#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.
--
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/052.358a913ee61d5c33033a57705469eebf%40djangoproject.com.