Tôi có biểu mẫu kế thừa từ 2 biểu mẫu khác. Trong biểu mẫu của tôi, tôi muốn thay đổi nhãn của một trường được xác định trong một trong các biểu mẫu gốc. Có ai biết làm thế nào để có thể xong không?Biểu mẫu Django - đặt nhãn
Tôi đang cố gắng làm điều đó trong số __init__
của mình, nhưng nó ném một lỗi nói rằng đối tượng "RegistrationFormTOS" không có thuộc tính 'email' ". Có ai biết làm thế nào tôi có thể làm điều này?
Cảm ơn.
Đây là mẫu mã của tôi:
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
Vấn đề với câu trả lời này là nó không giải thích làm thế nào để thay đổi 'nhãn của một trường đã được xác định trong một của các biểu mẫu gốc - biểu mẫu gốc là bit quan trọng. – jamesc
Nó không hoạt động đối với tôi ... '__init __() nhận được một đối số từ khóa không mong muốn 'label'' – User
Didnt' làm việc dưới dạng quản trị viên! – Raptor