如何获取DateField的标签? [英] How to get the label for DateField?

查看:39
本文介绍了如何获取DateField的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示字段DateField的标签?

How to display the label for the field DateField?

我的模型具有以下字段:

My model has the following field:

drum_period_1 = models.DateField(
    'qwerty',
    blank=True,
    null=True,
)

forms.py:

class DrumDataForm(forms.ModelForm):
    drum_period_1 = forms.DateField(
        widget=forms.TextInput(attrs={
            'class':'datepicker',
        }),
        required=False,
    )

    class Meta:
        model = UserProfile
        fields = (
            'drum_period_1', 
        )

template.html:

template.html:

<label class="label">{{ form.drum_period_1.label }}</label>

显示的标签为'drum_period_1',但必须显示'qwerty'.

The displayed label is 'drum_period_1', but 'qwerty' had to be shown.

推荐答案

根据文档:

字段的默认标签由字段名称通过以下方式生成将所有下划线都转换为空格,并在第一个大写字母之前信件.如果默认行为不会导致标签出现,请指定标签足够的标签.

The default label for a Field is generated from the field name by converting all underscores to spaces and upper-casing the first letter. Specify label if that default behavior doesn’t result in an adequate label.

label 参数传递给 DateField 构造函数:

Pass the label argument to the DateField constructor:

drum_period_1 = forms.DateField(
    widget=forms.TextInput(attrs={
        'class':'datepicker',
    }),
    required=False,
    label = "qwerty"
) 

或者,将 drum_period_1 重命名为 qwerty :

class DrumDataForm(forms.ModelForm):
    qwerty = forms.DateField(
        widget=forms.TextInput(attrs={
            'class':'datepicker',
        }),
        required=False,
    )

    class Meta:
        model = UserProfile
        fields = (
            'qwerty', 
        )

这篇关于如何获取DateField的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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