Django crispy forms - 为多个字段设置标签文本

Django crispy forms - Set label text for multiple fields

我正在研究 https://bixly.com/blog/awesome-forms-django-crispy-forms/ ,尝试使用 django crispy forms 设置 bootstrap 3 表单。

在app1/models.py中,我设置了我的表格:

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)
    ......

在 app1/forms.py 中我有:

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'))

现在,标签与字段名称相同。我怎样才能将标签设置为不同的东西。例如 'contact_name' 标签可能会询问 'What is your name?'

尝试使用 labels 元字段

喜欢:

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'))

其中 contact_name 是字段名称,'What is your name' 是要显示的输出