Django
1209 浏览 6 years, 1 month
6.5 media
版权声明: 转载请注明出处 http://www.codingsoho.com/field media
如果给widget添加css
方法1: 通过JavaScript添加css
<script>
$(document).ready(function(){
$(".field-area input").addClass("form-control");
$(".field-area textarea").addClass("form-control");
$(".field-area select").addClass("form-control");
$(".field-area>label").addClass("col-sm-2 control-label");
});
</script>
这种方法刷新网页时页面会二次布局,效果不好
方法2 :class继承
.field-area select{
@extend .form-control;
/*color: red;* method 4/
}
{% endblock %}
</style>
这种需要再LESS等技术中才支持
方法3:form中重写field
first_name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
方法4:在Form.Meta里面重写widget
class Meta:
widgets = {
'first_name': forms.TextInput(attrs={'class':'form-control'}),
}
方法5:在Form.init里面添加widget css属性
self.fields['first_name'].widget.attrs={"class":"form-control"}
或
self.fields['first_name'].widget.attrs['class'] = 'form-control'
参考:
- https://docs.djangoproject.com/en/1.8/topics/forms/media/
- https://stackoverflow.com/questions/14159577/apply-css-class-to-forms-radioselect-widget
- http://python.usyiyi.cn/documents/django_182/ref/forms/widgets.html