我有一个表单继承了其他两个表单.在我的表单中,我想更改在父表单中定义的字段的标签.有人知道怎么做吗?

我试图在我的__init__中这样做,但是它抛出一个错误,说"‘RegistrationFormTOS’对象没有‘email’属性".有人知道我怎么能做到这一点吗?

谢谢你.

这是我的表格代码:

from django import forms
from django.utils.translation import ugettext_lazy as _
from registration.forms import RegistrationFormUniqueEmail
from registration.forms import RegistrationFormTermsOfService

attrs_dict = { 'class': 'required' }

class RegistrationFormTOS(RegistrationFormUniqueEmail, RegistrationFormTermsOfService):
    """
    Subclass of ``RegistrationForm`` which adds a required checkbox
    for agreeing to a site's Terms of Service.

    """
    email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=75)), label=_(u'verify email address'))

    def __init__(self, *args, **kwargs):
        self.email.label = "New Email Label"
        super(RegistrationFormTOS, self).__init__(*args, **kwargs)

    def clean_email2(self):
        """
        Verifiy that the values entered into the two email fields
        match. 
        """
        if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
            if self.cleaned_data['email'] != self.cleaned_data['email2']:
                raise forms.ValidationError(_(u'You must type the same email each time'))
        return self.cleaned_data

推荐答案

你应该使用:

def __init__(self, *args, **kwargs):
    super(RegistrationFormTOS, self).__init__(*args, **kwargs)
    self.fields['email'].label = "New Email Label"

注意:首先你应该使用超级呼叫.

Django相关问答推荐

Django-Values()并按月分组

如何在Django中显示文件大小

设置wagail开发环境时出现ImportError:";无法从';wagta.search;";导入名称.

为特定表行更新或创建

新的 Django 开发人员无法在网站上显示Hello World,而不是默认安装成功页面

Django ImproperlyConfigured: AUTH_USER_MODEL指向尚未安装的模型'users.User'

Django Inline Tabular admin:删除一个不工作的对象

重建 Dockerfile 后,Django celery 无法从celery导入名称Celery

AttributeError:模块rest_framework.serializers在 Swagger 中的 Django 中没有属性NullBooleanField

如何在django中生成临时文件然后销毁

virtualenv(python3.4), pip install mysqlclient 错误

Django:使用render或render_to_response时添加响应头

django 模板中对象的模型名称

UnicodeEncodeError:ascii编解码器无法编码字符

如何使用查询参数构造 Django 反向/url?

django 如何知道已经运行了哪些迁移?

在 Django REST 框架序列化程序中动态排除或包含字段

django select_related - 何时使用它

如何在 Python 中运行另一个脚本而不等待它完成?

Python Django 的多线程