Django Formset中的自定义标签 [英] Custom Label in Django Formset

查看:57
本文介绍了Django Formset中的自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向表单集中添加自定义标签?

How do I add custom labels to my formset?

<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'中,如何设置它,使其显示为'您要唱歌的歌曲?'?

Now in the template instead of the field label being 'song', how do i set it so that it shows up as 'What song are you going to sing?'?

推荐答案

您可以在表单中使用 label 参数:

You can use the label argument in your form:

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

更新:

( @Daniel Roseman的评论)

或在模型中(使用 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 Formset中的自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆