Recently I struggled with a Django form. The requirement was to specify a placeholder
html data attribute. This task is very standard but the search terms involved made it
tricky to find a solution online. I provide it here to help others.
Let’s say you have an Event model. The description may be very long so you choose the TextField column type to store it.
class Event(gis_models.Model):
title = models.CharField()
description = models.TextField(blank=True, null=True)
We may then make a form class for our Event model.
class EventForm(forms.ModelForm):
description = forms.CharField(
required=False,
widget=forms.Textarea(
attrs={"placeholder": "Add a description of your event",}
),
)
class Meta:
model = Event
fields = [ "title", "description", ]
Let’s dissect this:
description = forms...
we would still get a working form input.CharField
which works for strings.Textarea
(note the case). It produces an html5 <textarea>
tag.forms.Textarea
accepts a keyword argument attrs
. This is a dictionary of html data attributes that you want to attach. Here we specify the placeholder
text.Meta
we are required to pass in either fields
or exclude
specifying what model fields we want in our form.For completeness here is a django template that would work with the previous form. It uses some bootstrap4.
<form action="/" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<formgroup>
{{ form.title.errors }}
{{ form.title.label_tag }}
{{ form.title }}
</formgroup>
<formgroup>
{{ form.description.errors }}
{{ form.description }}
</formgroup>
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
If you need help solving your business problems with software read how to hire me.