user1592380
user1592380

Reputation: 36215

Django crispy forms - Set label text for multiple fields

enter image description here

I'm working through https://bixly.com/blog/awesome-forms-django-crispy-forms/ , trying to set up a bootstrap 3 form using django crispy forms.

in app1/models.py, I have set up my form:

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django import forms


class User(AbstractUser):

    # Address
    contact_name = models.CharField(max_length=50)
    contact_address = models.CharField(max_length=50)
    contact_email = models.CharField(max_length=50)
    contact_phone = models.CharField(max_length=50)
    ......

In app1/forms.py I have:

class UserForm(forms.ModelForm):
    class Meta:
        model = User # Your User model
        fields = ['contact_name', 'contact_address', 'contact_email', 'contact_phone']
    helper = FormHelper()
    helper.form_method = 'POST'
    helper.add_input(Submit('Submit', 'Submit', css_class='btn-primary'))

Right now, the label is the same as the field name. How can I set the label to something different. Example for 'contact_name' the label might ask 'What is your name?'

Upvotes: 3

Views: 7973

Answers (1)

Marco Acierno
Marco Acierno

Reputation: 14847

Try to use the labels meta-field

Like:

class UserForm(forms.ModelForm):
    class Meta:
        model = User # Your User model
        fields = ['contact_name', 'contact_address', 'contact_email', 'contact_phone']
        labels = {
            'contact_name': 'What is your name',
        }
    helper = FormHelper()
    helper.form_method = 'POST'
    helper.add_input(Submit('Submit', 'Submit', css_class='btn-primary'))

where contact_name is the name of the field, and 'What is your name' is the output to show

Upvotes: 13

Related Questions