django定制表格标签

时间:2010-04-28 01:34:33

标签: django forms internationalization

我在使用Django表单自定义标签时遇到问题

这是文件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)

我使用的视图如下:

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")

用于初始化标签的字符串最终将取决于语言的字符串,即英语,荷兰语,法语等。

当我测试表单时,不会发送电子邮件,而是返回表单返回的重定向页面:

<QueryDict: {u'cc_myself': [u'on'], u'message': [u'message body'],
u'email':[u'info@umx.com'], u'subject': [u'test message']}>:

之前的主题标签。这显然是一个表示表单字段及其内容的字典。

当我将文件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)

即。禁用初始化然后一切正常。表单数据通过电子邮件发送,并显示重定向页面。显然,初始代码不对。但是什么?

我真的很感激一些帮助。

2 个答案:

答案 0 :(得分:2)

我认为你正在以错误的方式解决问题。您应该使用internationalisatoin模块。

这样你可以得到这样的表格:

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

如果您提供翻译文件,Django国际化系统将自动为您插入正确的翻译。

答案 1 :(得分:2)

您应该更改表单 init decleration,QueryDict得到打印的是您在初始化表单时作为第一个参数传递的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)
    ...

将解决您的问题。