我在django表格中面临一些问题。问题是即使我把这段代码" attrs = {' class':' form-control'}"但是当进入site和inspect-element时,html并没有包含这个类
它显示了这个......
anything
因为你可以看到它没有呈现出来':' form-control'
我可能做错了,请告诉我如何解决这个问题???
forms.py,view.py和.html文件代码如下所示
forms.py
anything.
contact.html
<input id="id_name" maxlength="100" name="name" type="text" required="">
views.py
from django import forms
from .models import ContactUs
class ContactUsForm(forms.ModelForm):
name = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': 'form-control'})),
email = forms.EmailField(required=True),
subject = forms.CharField(required=True),
message = forms.CharField(
required=True,
widget=forms.Textarea(attrs={'class': 'form-control'})
),
class Meta:
model = ContactUs
fields = ['name', 'email', 'subject', 'message']
答案 0 :(得分:0)
只需更新您的forms.py类,如下所示,它将添加表单字段属性。
class ContactUsForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ContactUsForm, self).__init__(*args, **kwargs)
self.fields['description'].widget = forms.TextInput(attrs={'class': 'form-control'})
self.fields['message'].widget = forms.Textarea(attrs={'class': 'form-control'})
name = forms.CharField(required=True)
email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
message = forms.CharField(required=True)
class Meta:
model = ContactUs
fields = ['name', 'email', 'subject', 'message']