I have a form that inherits from 2 other forms. In my form, I want to change the label of a field that was defined in one of the parent forms. Does anyone know how this can be done?
I’m trying to do it in my __init__
, but it throws an error saying that “‘RegistrationFormTOS’ object has no attribute ’email'”. Does anyone know how I can do this?
Thanks.
Here is my form code:
JavaScript
x
29
29
1
from django import forms
2
from django.utils.translation import ugettext_lazy as _
3
from registration.forms import RegistrationFormUniqueEmail
4
from registration.forms import RegistrationFormTermsOfService
5
6
attrs_dict = { 'class': 'required' }
7
8
class RegistrationFormTOS(RegistrationFormUniqueEmail, RegistrationFormTermsOfService):
9
"""
10
Subclass of ``RegistrationForm`` which adds a required checkbox
11
for agreeing to a site's Terms of Service.
12
13
"""
14
email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=75)), label=_(u'verify email address'))
15
16
def __init__(self, *args, **kwargs):
17
self.email.label = "New Email Label"
18
super(RegistrationFormTOS, self).__init__(*args, **kwargs)
19
20
def clean_email2(self):
21
"""
22
Verifiy that the values entered into the two email fields
23
match.
24
"""
25
if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
26
if self.cleaned_data['email'] != self.cleaned_data['email2']:
27
raise forms.ValidationError(_(u'You must type the same email each time'))
28
return self.cleaned_data
29
Advertisement
Answer
You should use:
JavaScript
1
4
1
def __init__(self, *args, **kwargs):
2
super(RegistrationFormTOS, self).__init__(*args, **kwargs)
3
self.fields['email'].label = "New Email Label"
4
Note first you should use the super call.