Django表单 - 标签 [英] Django form - set label
本文介绍了Django表单 - 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在我的 __ init __
抛出一个错误,说'RegistrationFormTOS'对象没有属性'email'。有没有人知道我可以这样做?
谢谢。
这是我的表单代码:
从django导入表单
从django.utils.translation导入ugettext_lazy作为_
从registration.forms导入RegistrationFormUniqueEmail
from registration.forms import RegistrationFormTermsOfService
attrs_dict = {'class':'required'}
class RegistrationFormTOS(RegistrationFormUniqueEmail,RegistrationFormTermsOfService):
RegistrationForm的子类,它添加了一个必需的复选框
来同意一个网站的服务条款
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 =新电子邮件标签
super(RegistrationFormTOS,self).__ init __(* args,** kwargs)
def clean_email2 (自):
验证输入到两个电子邮件字段
中的值是否匹配。
如果self.cleaned_data中的email和self.cleaned_data中的email2:
如果self.cleaned_data ['email']!= self.cleaned_data ['email2' ]:
raise forms.ValidationError(_(你必须每次输入相同的电子邮件))
return self.cleaned_data
解决方案
您应该使用
def __init __(self,* args,** kwargs):
super(RegistrationFormTOS,self).__ init __(* args,** kwargs)
self.fields ['email']。label =新电子邮件标签
首先应该使用超级电话。
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:
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
解决方案
You should use:
def __init__(self, *args, **kwargs):
super(RegistrationFormTOS, self).__init__(*args, **kwargs)
self.fields['email'].label = "New Email Label"
Note first you should use the super call.
这篇关于Django表单 - 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文