删除Django Crispy Forms中的标签

时间:2021-03-04 16:00:09

Does anybody know if there is a correct way to remove labels in a crispy form?

有人知道是否有正确的方法来清除香脆的标签?

I got as far as this:

我得到了这个:

self.fields['field'].label = ""

But it's not a very nice solution.

但这不是一个很好的解决方案。

4 个解决方案

#1


5  

You could edit the field.html template: https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7

您可以编辑field.html模板:https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7

Add a FormHelper attribute to your form that controls the label rendering and use it in that template if. Custom FormHelper attributes are not yet officially documented, because I haven't had time, but I talked about them in a keynote I gave, here are the slides: https://speakerdeck.com/u/maraujop/p/django-crispy-forms

将FormHelper属性添加到控制标签呈现的表单,并在该模板中使用它。自定义FormHelper属性尚未正式记录,因为我没有时间,但我在主题演讲中谈过它们,这里有幻灯片:https://speakerdeck.com/u/maraujop/p/django-crispy -形式

#2


28  

Just do:

做就是了:

self.helper.form_show_labels = False

To remove all labels.

删除所有标签。

#3


4  

if you are only to remove some labels from input, then explicitly don't give a label name in model definition, i.e:

如果你只想从输入中删除一些标签,那么明确地不要在模型定义中给出标签名称,即:

field = models.IntegerField("",null=True)

#4


4  

Works with Boostrap ( see documentation )

与Boostrap一起使用(参见文档)

In your form :

在你的形式:

from crispy_forms.helper import FormHelper
from django import forms

class MyForm(forms.Form):
    [...]
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_show_labels = False 

In your template:

在您的模板中:

<form method='POST' action=''>{% csrf_token %}
{% crispy form %}
<input type='submit' value='Submit' class='btn btn-default'>
</form>

#1


5  

You could edit the field.html template: https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7

您可以编辑field.html模板:https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7

Add a FormHelper attribute to your form that controls the label rendering and use it in that template if. Custom FormHelper attributes are not yet officially documented, because I haven't had time, but I talked about them in a keynote I gave, here are the slides: https://speakerdeck.com/u/maraujop/p/django-crispy-forms

将FormHelper属性添加到控制标签呈现的表单,并在该模板中使用它。自定义FormHelper属性尚未正式记录,因为我没有时间,但我在主题演讲中谈过它们,这里有幻灯片:https://speakerdeck.com/u/maraujop/p/django-crispy -形式

#2


28  

Just do:

做就是了:

self.helper.form_show_labels = False

To remove all labels.

删除所有标签。

#3


4  

if you are only to remove some labels from input, then explicitly don't give a label name in model definition, i.e:

如果你只想从输入中删除一些标签,那么明确地不要在模型定义中给出标签名称,即:

field = models.IntegerField("",null=True)

#4


4  

Works with Boostrap ( see documentation )

与Boostrap一起使用(参见文档)

In your form :

在你的形式:

from crispy_forms.helper import FormHelper
from django import forms

class MyForm(forms.Form):
    [...]
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_show_labels = False 

In your template:

在您的模板中:

<form method='POST' action=''>{% csrf_token %}
{% crispy form %}
<input type='submit' value='Submit' class='btn btn-default'>
</form>