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

时间:2016-08-02 15:00:15

标签: python django django-crispy-forms

enter image description here

我正在使用https://bixly.com/blog/awesome-forms-django-crispy-forms/,尝试使用django crispy表单设置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'的示例标签可能会询问“你的名字是什么?”'

1 个答案:

答案 0 :(得分:6)

尝试使用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'是要显示的输出