Specifying labels

By default, the labels on Django's auto-generated form HTML are created by replacing underscores with spaces and capitalizing the first letter-so the label for the email field is "Email". (Sound familiar? It's the same simple algorithm that Django's models use to calculate default verbose_name values for fields. We covered this in Chapter 4, Models). But, as with Django's models, we can customize the label for a given field. Just use label, like so:

class ContactForm(forms.Form): 
    subject = forms.CharField(max_length=100) 
    email = forms.EmailField(required=False,
        label='Your e-mail address') 
    message = forms.CharField(widget=forms.Textarea)

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.