django customizing form labels -
i have problem in customizing labels in django form
this form code in file contact_form.py:
from django import forms class contactform(forms.form): def __init__(self, subject_label="subject", message_label="message", email_label="your email", cc_myself_label="cc myself", *args, **kwargs): super(contactform, self).__init__(*args, **kwargs) self.fields['subject'].label = subject_label self.fields['message'].label = message_label self.fields['email'].label = email_label self.fields['cc_myself'].label = cc_myself_label subject = forms.charfield(widget=forms.textinput(attrs={'size':'60'})) message = forms.charfield(widget=forms.textarea(attrs={'rows':15, 'cols':80})) email = forms.emailfield(widget=forms.textinput(attrs={'size':'60'})) cc_myself = forms.booleanfield(required=false)
the view using in looks like:
def contact(request, product_id=none): . . . if request.method == 'post': form = contact_form.contactform(request.post) if form.is_valid(): . . else: form = contact_form.contactform( subject_label = "subject", message_label = "your message", email_label = "your email", cc_myself_label = "cc myself")
the strings used initializing labels strings dependent on language, i.e. english, dutch, french etc.
when test form email not sent , instead of redirect-page form returns with:
<querydict: {u'cc_myself': [u'on'], u'message': [u'message body'], u'email':[u'info@umx.com'], u'subject': [u'test message']}>:
where subject label before. dictionary representing form fields , contents.
when change file contact_form.py into:
from django import forms class contactform(forms.form): """ def __init__(self, subject_label="subject", message_label="message", email_label="your email", cc_myself_label="cc myself", *args, **kwargs): super(contactform, self).__init__(*args, **kwargs) self.fields['subject'].label = subject_label self.fields['message'].label = message_label self.fields['email'].label = email_label self.fields['cc_myself'].label = cc_myself_label """ subject = forms.charfield(widget=forms.textinput(attrs={'size':'60'})) message = forms.charfield(widget=forms.textarea(attrs={'rows':15, 'cols':80})) email = forms.emailfield(widget=forms.textinput(attrs={'size':'60'})) cc_myself = forms.booleanfield(required=false)
i.e. disabling initialization works. form data sent email , redirect page shows up. the init code isn't right. what?
i appreciate help.
you should change form init decleration, querydict tha gets printed request.get or request.post pass first argument when initialize form.
i guess changing this
def __init__(self, subject_label="subject", ...
to this
def __init__(self, data=none, subject_label="subject", ... ...): super(contactform, self).__init__(data, *args, **kwargs) ...
will solve problem.
Comments
Post a Comment