Ho un modulo che eredita da 2 altri moduli. Nella mia forma, voglio cambiare l'etichetta di un campo che è stato definito in una delle forme padre. qualcuno sa come questo potrebbe essere fatto?Modulo Django - set etichetta
Sto provando a farlo nel mio __init__
, ma genera un errore che dice che l'oggetto "'RegistrationFormTOS' non ha attributo 'email'". Qualcuno sa come posso fare questo?
Grazie.
Ecco il mio codice del modulo:
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
Il problema di questa risposta è che non spiega come cambiare 'l'etichetta di un campo che è stato definito in una delle forme padre - la forma genitore è il bit importante. – jamesc
Non ha funzionato per me ... '__init __() ha ottenuto un argomento di parole chiave inaspettato 'label'' – User
Non funziona in forma di amministratore! – Raptor