I am working on a project, and my designer gave me the design, so I want to mimic the input forms which are in the design, to do that I need to add css in my form object.
If in the case of django function based view, It is easy to addcss by creating forms.py files and add css as follows. adding css class in django forms
I also tried to inject css class using jquery, but I couldn't succed. Injecting css class in html forms element using jquery
But how do I add custom css in the form field, when using class based view.
class ItemUpdateView(UpdateView):
model = Item
fields = ('name','tag', 'category', 'company', 'price',)
How do I add the custom class to name, tag, category
field.
答案 0 :(得分:3)
You can define form_class attribute on your update view and add css to that form.
class ItemUpdateView(UpdateView):
form_class = ItemForm
ItemForm can be a ModelForm and you can customize it as described here: https://docs.djangoproject.com/es/1.9/topics/forms/modelforms/#overriding-the-default-fields
答案 1 :(得分:1)
当django从表单字段生成html时,它会自动将其添加为“ id_fieldName”。因此,您可以在前端使用javascript添加CSS。
<script>
document.getElementById("id_company").classList.add("class name");
</script>