Contact form Labels - is there a difference to adding labels directly in function?


#1

Hey,

I was wondering if there is a specific reason why we should add the contact form labels like this:

1 class ContactForm(forms.Form):
2 contact_name = forms.CharField(required=True)
3 contact_email = forms.EmailField(required=True)
4 content = forms.CharField(
5 required=True,
6 widget=forms.Textarea
7 )
8
9 # the new bit we’re adding
10 def init(self, *args, **kwargs):
11 super(ContactForm, self).init(*args, **kwargs)
12 self.fields[‘contact_name’].label = "Your name:"
13 self.fields[‘contact_email’].label = "Your email:"
14 self.fields[‘content’].label = “What do you want to say?”

Instead of just adding them like this :

custom contact form

class ContactForm(forms.Form):
contact_name = forms.CharField(label=‘Your name’)
contact_email = forms.EmailField(label=‘Your E-Mail’)
content = forms.CharField(widget=forms.Textarea, label=‘What do you want to say?’)

Is there good reason for doing the prior or doesn’t it matter?

Thanks
Roxy


#2

Yeah there are a thousand ways of doing things in Django — if both work, then they’re equivalent and you can do either. :)