gpt4 book ai didi

django - Django Formset 中的自定义标签

转载 作者:行者123 更新时间:2023-12-04 13:12:15 24 4
gpt4 key购买 nike

如何将自定义标签添加到我的表单集中?

<form method="post" action="">

{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{{ field.label_tag }}: {{ field }}
{% endfor %}
{% endfor %}
</form>

我的模型是:
class Sing(models.Model):
song = models.CharField(max_length = 50)
band = models.CharField(max_length = 50)

现在在模板中而不是字段标签是 'song' ,我如何设置它以使其显示为 'What song are you going to sing?' ?

最佳答案

您可以使用 label以你的形式论证:

class MySingForm(forms.Form):
song = forms.CharField(label='What song are you going to sing?')
...
如果您正在使用 ModelForms :
class MySingForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MySingForm, self).__init__(*args, **kwargs)
self.fields['song'].label = 'What song are you going to sing?'

class Meta:
model = Sing
更新:
(@丹尼尔罗斯曼的评论)
或者在模型中(使用 verbose_name ):
class Sing(models.Model):
song = models.CharField(verbose_name='What song are you going to sing?',
max_length=50)
...
或者
class Sing(models.Model):
song = models.CharField('What song are you going to sing?', max_length=50)
...

关于django - Django Formset 中的自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com