【问题标题】:django crispy forms label override not workingdjango 脆表单标签覆盖不起作用
【发布时间】:2017-02-19 08:07:41
【问题描述】:

我似乎无法用 django 脆皮表单覆盖默认标签。

模型.py

class User(AbstractUser):
    house_name_number = models.CharField(max_length=255)
    street_name = models.CharField(max_length=255)
    town_city = models.CharField(max_length=255)
    county = models.CharField(max_length=255)
    postcode = models.CharField(max_length=8)
    same_address = models.BooleanField()  
    move_in_date = models.DateField(null=True)

forms.py

class AddressForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.label_class = 'sr-only'
        self.helper.form_tag = False
        self.helper.layout = Layout(
            PrependedText('postcode', '<i class="fa fa-home"></i>', placeholder="Postcode", autofocus=""),
            PrependedText('house_name_number', '<i class="fa fa-home"></i>', placeholder="Building Number or Name",
                          ),
            PrependedText('street_name', '<i class="fa fa-home"></i>', placeholder="Street Name",
                          ),
            PrependedText('town_city', '<i class="fa fa-home"></i>', placeholder="Town or City",
                          label="test"),
            PrependedText('county', '<i class="fa fa-home"></i>', placeholder="County"),
            Field('same_address', '<i class="fa fa-home"></i>',
                  label="Have you lived at the property for 3 years"),
            PrependedText('move_in_date', '<i class="fa fa-calendar"></i>', required=False,
                          placeholder="What date did you move in to your current address"),
        )

【问题讨论】:

标签: django django-crispy-forms


【解决方案1】:

我建议在表单的__init__ 方法中设置自定义标签:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['town_city'].label = 'Test'
    ...

label="something" 添加到 Crispy Forms 字段将导致输入字段上出现“标签”属性,这不是您想要的。

【讨论】:

    【解决方案2】:

    不知道为什么脆的表单覆盖不起作用。但我使用class Meta: 和标签选项 docs.djangoproject.com/en/dev/topics/forms/modelforms/...

    class AddressForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.label_class = 'sr-only'
        self.helper.form_tag = False
        self.helper.layout = Layout(
            PrependedText('house_name_number', '<i class="fa fa-home"></i>', placeholder="Building Number or Name",
                          ),
            PrependedText('street_name', '<i class="fa fa-home"></i>', placeholder="Street Name",
                          ),
            PrependedText('town_city', '<i class="fa fa-home"></i>', placeholder="Town or City",
                          label="test"),
            PrependedText('county', '<i class="fa fa-home"></i>', placeholder="County"),
            PrependedText('postcode', '<i class="fa fa-home"></i>', placeholder="Postcode", autofocus=""),
            Field('same_address', ),
            PrependedText('move_in_date', '<i class="fa fa-calendar"></i>',
                          placeholder="What date did you move in to your current address"),
        )
    
    class Meta:
        model = User
        fields = ['house_name_number',
                  'street_name',
                  'town_city',
                  'county',
                  'postcode',
                  'same_address',
                  'move_in_date',
                  ]
        labels = {
            'same_address': 'Have you lived at the property for 3 years?',
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2017-07-16
      • 2020-09-17
      • 2018-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 2019-04-24
      相关资源
      最近更新 更多