Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django crispy表单标签覆盖不工作_Django_Django Crispy Forms - Fatal编程技术网

django crispy表单标签覆盖不工作

django crispy表单标签覆盖不工作,django,django-crispy-forms,Django,Django Crispy Forms,我似乎无法用django crispy表单覆盖默认标签 model.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(

我似乎无法用django crispy表单覆盖默认标签

model.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"),
        )
类地址表单(forms.ModelForm):
定义初始化(self,*args,**kwargs):
super()
self.helper=FormHelper(self)
self.helper.label_class='sr only'
self.helper.form_标记=False
self.helper.layout=布局(
PrependedText('postcode','',placeholder=“postcode”,autofocus=”),
PrependedText('房屋名称\编号','',占位符=“房屋编号或名称”,
),
前置文本('街道名称','',占位符=“街道名称”,
),
预设文本('town\u city','',placeholder=“town或city”,
label=“test”),
PrependedText('county','',placeholder=“county”),
字段('相同的地址','',
label=“您是否在该物业居住了3年”),
PrependedText('move_in_date','',必需=False,
placeholder=“您入住当前地址的日期”),
)

不知道为什么crispy表单覆盖不起作用。但是我使用
类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?',
    }
类地址表单(forms.ModelForm):
定义初始化(self,*args,**kwargs):
super()
self.helper=FormHelper(self)
self.helper.label_class='sr only'
self.helper.form_标记=False
self.helper.layout=布局(
PrependedText('房屋名称\编号','',占位符=“房屋编号或名称”,
),
前置文本('街道名称','',占位符=“街道名称”,
),
预设文本('town\u city','',placeholder=“town或city”,
label=“test”),
PrependedText('county','',placeholder=“county”),
PrependedText('postcode','',placeholder=“postcode”,autofocus=”),
字段('same_address',),
PrependedText('move_in_date','',
placeholder=“您入住当前地址的日期”),
)
类元:
模型=用户
字段=['房屋名称\编号',
“街道名称”,
“城市”,
"县",,
“邮政编码”,
“同一地址”,
“移入日期”,
]
标签={
“同一地址”:“您在该房产住了3年了吗?”,
}

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

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

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

不知道Crispy form覆盖为什么不起作用。但是我使用
类Meta:
和标签选项做了一个变通方法