Django form-set label
Mam formularz, który dziedziczy z 2 innych form. W moim formularzu chcę zmienić etykietę pola zdefiniowanego w jednym z formularzy nadrzędnych. Czy ktoś wie jak można to zrobić?
Próbuję to zrobić w moim __init__
, ale wyświetla błąd mówiący, że "obiekt' RegistrationFormTOS 'nie ma atrybutu 'email'". Czy ktoś wie, jak Mogę to zrobić?
Oto Mój kod formularza:
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
5 answers
Powinieneś użyć:
def __init__(self, *args, **kwargs):
super(RegistrationFormTOS, self).__init__(*args, **kwargs)
self.fields['email'].label = "New Email Label"
Uwaga najpierw należy użyć super call.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-12 01:06:40
Oto przykład zaczerpnięty z nadpisywania pól domyślnych :
from django.utils.translation import ugettext_lazy as _ class AuthorForm(ModelForm): class Meta: model = Author fields = ('name', 'title', 'birth_date') labels = { 'name': _('Writer'), } help_texts = { 'name': _('Some useful help text.'), } error_messages = { 'name': { 'max_length': _("This writer's name is too long."), }, }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-01 19:00:31
Dostęp do pól w formularzu odbywa się za pomocą dict 'fields':
self.fields['email'].label = "New Email Label"
Dzięki temu nie musisz się martwić o to, że pola formularza mają nazwy zderzające się z metodami klasy formularza. (W przeciwnym razie nie można mieć pola o nazwie 'clean' lub 'is_valid') Definiowanie pól bezpośrednio w ciele klasy jest głównie wygodą.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-12 01:06:37
Możesz ustawić label
jako atrybut pola podczas definiowania formularza.
class GiftCardForm(forms.ModelForm):
card_name = forms.CharField(max_length=100, label="Cardholder Name")
card_number = forms.CharField(max_length=50, label="Card Number")
card_code = forms.CharField(max_length=20, label="Security Code")
card_expirate_time = forms.CharField(max_length=100, label="Expiration (MM/YYYY)")
class Meta:
model = models.GiftCard
exclude = ('price', )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-09 04:14:27
To nie działa dla dziedziczenia modelu, ale możesz ustawić etykietę bezpośrednio w modelu
email = models.EmailField("E-Mail Address")
email_confirmation = models.EmailField("Please repeat")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-15 13:00:13