默认情况下,我的django ModelForm生成一个HTML表单。我想通过在select元素的附加属性修改窗体的HTML输出:修改Django的HTML输出ModelForm
# models.py
class FoodForm(ModelForm):
class Meta:
model = Food
fields = ('name', 'ingredients')
# default HTML output
Name:
Ingredients:
Mozzarella
Cottage cheese
Onion
我想添加一个新属性到选择栏和离开当前属性不变:
# desired HTML output
我试着用小工具做到这一点(见下文),但它给我一个错误:
class FoodForm(ModelForm):
class Meta:
model = Food
fields = ('name', 'ingredients')
widgets = {
'ingredients': Select(attrs={'data-placeholder': "Choose ingredients..."}),
}
# NameError: name 'Select' is not defined
任何想法如何修改HTML输出?
2012-11-09
finspin