0

I'm struggling with a django custom widget used to present HTML data.

I've started with a template from standard django's textarea:

class Descriptionarea(forms.Widget):
    template_name = "widgets/descriptionarea.html"

    def __init__(self, attrs=None):
        super().__init__(attrs

widgets/descriptionarea.html:

<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>

Which works fine as an element of my inline form:

class InlineForm(forms.ModelForm):
    description = forms.CharField(widget=Descriptionarea)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

which is part of my TabularInline (django-nested-admin in use):

class TabularInline(NestedStackedInline):
    form = InlineForm
    fields = ("title", "description")
    readonly_fields = ("title", )
    model = MyModel
    max_num = 0

I'd like to present the description as HTML document, so I've changed the template:

<details>
<summary>description</summary>
<pre name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% if widget.value %}{{ widget.value | safe }}{% endif %}
</pre>
</details>

Unfortunately, it does not work, as now I cannot use save on admin pages, as Django complains about:

This field is required.

Indeed during save, the whole widget is cleared out. Second question - when I add the description field to the list of readonly_fields, then it becomes a simple label. Is it possible to provide my own readonly widget?

user3193620
  • 173
  • 1
  • 11

0 Answers0