163👍
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.
66👍
Here’s an example taken from Overriding the default fields:
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."), }, }
- [Django]-How do you use the django-filter package with a list of parameters?
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)
- [Django]-How do I match the question mark character in a Django URL?
23👍
You can set label
as an attribute of field when you define form.
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', )
- [Django]-How to implement followers/following in Django
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-How to find pg_config path
9👍
You access fields in a form via the ‘fields’ dict:
self.fields['email'].label = "New Email Label"
That’s so that you don’t have to worry about form fields having name clashes with the form class methods. (Otherwise you couldn’t have a field named ‘clean’ or ‘is_valid’) Defining the fields directly in the class body is mostly just a convenience.
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
- [Django]-Sending HTML email in django
- [Django]-Django + Ajax
3👍
Try on Models.py
email = models.EmailField(verbose_name="E-Mail Address")
email_confirmation = models.EmailField(verbose_name="Please repeat")
- [Django]-How can I use Django permissions without defining a content type or model?
- [Django]-Django url tag multiple parameters
- [Django]-Get all related Django model objects
2👍
It don’t work for model inheritance, but you can set the label directly in the model
email = models.EmailField("E-Mail Address")
email_confirmation = models.EmailField("Please repeat")
- [Django]-Django check for any exists for a query
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
1👍
While using a class’ constructor to change the field label for an instance works, it’s better from an object-oriented design standpoint to make the change at the class level if the label doesn’t need to be customized per-instance.
For example, the following will change the label on PasswordChangeForm‘s new_password2 field from "New password confirmation" to "Confirm Password":
class MyPasswordChangeForm(PasswordChangeForm):
PasswordChangeForm.base_fields['new_password2'].label = 'Confirm Password'
Take a look in django/forms/forms.py, you’ll see that DeclarativeFieldsMetaclass removes the fields from a class’ attribute list and places them in base_fields and declared_fields.
class Form(BaseForm, metaclass=DeclarativeFieldsMetaclass):
...
class DeclarativeFieldsMetaclass(MediaDefiningClass):
"""Collect Fields declared on the base classes."""
def __new__(mcs, name, bases, attrs):
# Collect fields from current class and remove them from attrs.
attrs['declared_fields'] = {
key: attrs.pop(key) for key, value in list(attrs.items())
if isinstance(value, Field)
}
new_class = super().__new__(mcs, name, bases, attrs)
# Walk through the MRO.
declared_fields = {}
for base in reversed(new_class.__mro__):
# Collect fields from base class.
if hasattr(base, 'declared_fields'):
declared_fields.update(base.declared_fields)
# Field shadowing.
for attr, value in base.__dict__.items():
if value is None and attr in declared_fields:
declared_fields.pop(attr)
new_class.base_fields = declared_fields
new_class.declared_fields = declared_fields
There’s a comment in BaseForm that provides further explanation:
class BaseForm:
...
# The base_fields class attribute is the *class-wide* definition of
# fields. Because a particular *instance* of the class might want to
# alter self.fields, we create self.fields here by copying base_fields.
# Instances should always modify self.fields; they should not modify
# self.base_fields.
- [Django]-Import data from excel spreadsheet to django model
- [Django]-Django ignores router when running tests?
- [Django]-Django – Website Home Page
0👍
if all other solutions don’t work, this one worked for me (to change the user class label to django contrib auth)
#in models.py
User._meta.get_field('username').verbose_name = "new name"
- [Django]-Equivalent of PHP "echo something; exit();" with Python/Django?
- [Django]-How to manually assign imagefield in Django
- [Django]-"{% extends %}" and "{% include %}" in Django Templates