【发布时间】:2012-07-13 10:08:52
【问题描述】:
有人知道是否有正确的方法可以去除脆皮标签吗?
我已经做到了:
self.fields['field'].label = ""
但这不是一个很好的解决方案。
【问题讨论】:
标签: django forms label django-crispy-forms
有人知道是否有正确的方法可以去除脆皮标签吗?
我已经做到了:
self.fields['field'].label = ""
但这不是一个很好的解决方案。
【问题讨论】:
标签: django forms label django-crispy-forms
只要做:
self.helper.form_show_labels = False
删除所有标签。
【讨论】:
self.fields['MYFIELD'].label = False 禁用特定字段
form_show_labels = False 并用self.fields['validation_CGU'].label = True 覆盖了一个特定字段,但它没有工作,似乎全局规则优先,太糟糕了
适用于 Boostrap (see documentation)
在你的表格中:
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
在您的模板中:
<form method='POST' action=''>{% csrf_token %}
{% crispy form %}
<input type='submit' value='Submit' class='btn btn-default'>
</form>
【讨论】:
您可以编辑field.html 模板:
https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7
将FormHelper 属性添加到控制标签呈现的表单并在该模板if 中使用它。自定义FormHelper 属性尚未正式记录,因为我没有时间,但我在我给出的主题演讲中谈到了它们,以下是幻灯片:
https://speakerdeck.com/u/maraujop/p/django-crispy-forms
【讨论】:
page not found
以下解决方案可让您从常规控件或脆控件中删除标签。不仅标签文本消失了,而且标签使用的空间也被删除了,这样你就不会得到一个空白标签占用空间并弄乱你的布局。
以下代码适用于 django 2.1.1。
# this class would go in forms.py
class SectionForm(forms.ModelForm):
# add a custom field for calculation if desired
txt01 = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
''' remove any labels here if desired
'''
super(SectionForm, self).__init__(*args, **kwargs)
# remove the label of a non-linked/calculated field (txt01 added at top of form)
self.fields['txt01'].label = ''
# you can also remove labels of built-in model properties
self.fields['name'].label = ''
class Meta:
model = Section
fields = "__all__"
我不清楚 OP 对他展示的代码 sn-p 有什么问题,只是他没有将代码行放在正确的位置。这似乎是最好和最简单的解决方案。
【讨论】:
如果您只是从输入中删除一些标签,那么在模型定义中明确不要给出标签名称,即:
field = models.IntegerField("",null=True)
【讨论】:
删除所有标签:
self.helper.form_show_labels = False
全部为假时显示特定标签:
HTML('<span>Your Label</span>')
禁用特定字段的标签当一切为真时
self.fields['fieldName'].label = True
示例:
Row(
HTML('<span> Upolad Government ID (Adhar/PAN/Driving Licence)</span>'),
Column('IdProof',css_class='form-group col-md-12 mb-0'),
css_class='form-row'
),
【讨论】: