django 脆皮表单标签覆盖不起作用

标签 django django-crispy-forms

我似乎无法用 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)

表单.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"),
        )

最佳答案

不知道为什么脆皮表单覆盖不起作用。但我使用 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?',
    }

关于django 脆皮表单标签覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966128/

相关文章:

json - 将有效负载预处理到 Django 中的惯用方法是什么?

angularjs - Django RestAuth 自定义密码重置链接

python - Django Cripy-forms field_class 未按预期工作

python - django-crispy form - 隐藏字段标签

django - 无法将 Helper 传递给模板中的 django Crispy Formset

Django - 以 ManytoMany 形式显示图像字段而不是标题

python - Django:反转 'detail',参数为 '(' ',)' and keyword arguments ' {}' 未找到

php - 在 MySQL 中执行查询的性能。也许正在使用缓存?

python - Wagtail:部署问题 -/admin 上的 KeyError 'request'

python - Django Crispy Forms 类型错误 : __init__() got an unexpected keyword argument 'instance'