Changing how fields are rendered
Probably the first thing you'll notice when you render this form locally is that the message
field is displayed as an <input type="text">
, and it ought to be a <textarea>
. We can fix that by setting the field's widget:
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField()
email = forms.EmailField(required=False)
message = forms.CharField(widget=forms.Textarea)
The forms framework separates out the presentation logic for each field into a set of widgets. Each field type has a default widget, but you can easily override the default, or provide a custom widget of your own. Think of the Field
classes as representing validation logic, while widgets represent presentation logic.
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.