如何显示某些特定的列?

时间:2019-08-12 14:34:33

标签: python django django-models django-forms

我有这个问题:

我有以下使用Django的表格:

class Test(forms.Form):


    name = forms.CharField(
        label=_('Name'),
    )
    price = forms.IntegerField(  
        label=_('Price'),
        required=False,
    )
    year = forms.IntegerField(
        label=_('year'),
        required=False,
    )

然后显示带有条目的标签,我这样做:

{% if form %}
    {{ form.as_p }}

但是我只想在输入项中显示标签“名称”,在输入项中显示标签“价格”,并为年份提供默认值。

能帮我吗?

非常感谢您!

PS:我尝试使用此editable:False,但得到了一个意想不到的关键字参数'editable'

1 个答案:

答案 0 :(得分:0)

要以Django形式提供默认值,请像这样使用initial-

class Test(forms.Form):
    year = forms.IntegerField(
        label=_('year'),
        required=False,
        initial=33445677
    )
    # disable a field by using disabled=True in your form
    name = forms.CharField(
        label=_('Name'),
        disabled=True,
    )

了解有关初始here

的信息