I'm trying to override widgets (like textarea) globally in Django.
For this, I defined my own django/form/widgets/textarea.html
file.
First, I wanted to modify the default number of rows. The textarea.html
file will looked like this:
<textarea
rows="3"
name="{{ widget.name }}"
{% include "django/forms/widgets/attrs.html" %}
>{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
This example above works perfectly.
But what if I want to add a custom class now?
<textarea
rows="3"
class="my-custom-class"
name="{{ widget.name }}"
{% include "django/forms/widgets/attrs.html" %}
>{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
This won't work as expected because the attrs.html
is supposed to handle extra attributes like class
. Hence, defining a class attribute directly in textarea would break this behaviour and would erase existing classes.
What is the "clean" way to add extra classes globally? Is is acceptable to totally get rid of the include?
I'd like to do it in HTML, not in Python. For example, I don't want to define a new Widget which would inherit from the base widget when I'm defining my forms, as I think this is pure markup topic, and should not interfere with form definition itself.
EDIT : I suspect this package could help
Thanks.