Customizing django form shortcuts

I have a problem setting labels in a Django form

This is the form code in 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 I am using is as follows:

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 to initialize the shortcuts will ultimately contain strings depending on the language, such as English, Dutch, French, etc.

When I test the form, the email is not sent, and instead of the redirect page, the 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 the label of the object was previously located. This is obviously a dictionary representing form fields and their contents.

When I change the contact_form.py file to:

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, then everything will work. The form data is sent by email and a redirect page appears. Obviously, the init code is wrong. But what?

I would really appreciate help.

+3
2

init, QueryDict tha request.GET request.POST, .

,

def __init__(self, subject_label="Subject", ...

def __init__(self, data=None, subject_label="Subject", ... ...):
    super(ContactForm, self).__init__(data, *args, **kwargs)
    ...

.

+2

, . internationalisatoin.

, :

class ContactForm(forms.Form):
    subject = forms.CharField(label=_('contact_form_subject'), widget=forms.TextInput(attrs={'size':'60'}))

Django , .

+2

Source: https://habr.com/ru/post/1743097/


All Articles