【发布时间】:2017-10-21 13:07:03
【问题描述】:
我正在尝试将 CSS 应用于我的 html 模板上的各个 {{form}} 字段。 我引用了这个已解决的问题:
使用答案,我创建了 forms.py 页面,如下所示:
from django import forms
from .models import ContactInfo
class ContactForm(forms.ModelForm):
# class meta brings in the fields from models
class Meta:
model = ContactInfo
# grab the fields from models ContactInfo class
fields = ('Fullname')
# specify which bootstrap class each html widget should take
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['Fullname'].widget.attrs.update({'class': 'form-control'})
使用 {{form}} 元素的html:
<div class="form-group">
<label for="id_Fullname">Full Name:</label>
{{form.Fullname}}
</div>
不使用 {{form}} 的工作 html:
<div class="form-group">
<label for="fullname">Full Name:</label>
<input type="text" class="form-control" id="fullname">
</div>
如何让“标签”标签指向 {{form.Fullname}} 中的 id。我的猜测是它现在没有这样做,因此它不应用 css。我在 forms.py 的小部件中指定的那个或那个类没有按我想要的方式工作。
【问题讨论】:
标签: django django-forms django-templates django-views