尽管小部件规范,Django模型表使用默认值?

时间:2015-10-04 20:58:19

标签: python django

我定义了以下表格:

[
    {
        "pk": 1,
        "model": "companies.Company",
        "fields": {
            "name": "Google",
            "site": "google.com"
        }
    },
    {
        "pk": 2,
        "model": "companies.Company",
        "fields": {
            "name": "Zoho",
            "site": "zoho.com"
        }
    },
    {
        "pk": 3,
        "model": "companies.Company",
        "fields": {
            "name": "Digg",
            "site": "digg.com"
        }
    }
]

然而,当我试图看看表格的样子时:

from django import forms
from my_app.models import Business


class BusinessNameForm(forms.models.ModelForm):

    class Meta:
        model = Business
        fields = ('business_name',)
        widgets = {
        'text': forms.fields.TextInput(attrs={
            'placeholder': 'Enter a business name',
            'class': 'form-control input-lg',
            }),
        }

它给了我以下HTML:

f = BusinessName()
f.as_p()

为什么不设置占位符和类属性?

1 个答案:

答案 0 :(得分:2)

您应该使用字段名称作为小部件词典中的键。

widgets = {
    'business_name': forms.fields.TextInput(attrs={
        'placeholder': 'Enter a business name',
        'class': 'form-control input-lg',
    }),
}

请参阅the docs