Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add class to form field Django ModelForm

I am trying to write a Bootstrap Form with Django ModelForm. I have read the Django Documentation Django Documentation about Forms, so I have this code:

<div class="form-group"> {{ form.subject.errors }} <label for="{{ form.subject.id_for_label }}">Email subject:</label> {{ form.subject }}</div> 

The {{form.subject}} is rendered by Django, for example in CharField field model, as input tag,

<input type="text"....> etc. 

I need add "form-control" class to every input in order to get Bootstrap input appearance (without third-party packages). I found this solution Django add class to form <input ..> field. Is there any way to add a class to every field by default without specifying it in every attribute of the class of Form class?

class ExampleForm(forms.Form):    name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))    email = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))    address = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))    country = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'})) 

and so on ..

like image 626
José Luis Avatar asked Apr 18 '15 10:04

José Luis


People also ask

What is ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

What is the difference between form and ModelForm in Django?

The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database. Show activity on this post. Show activity on this post. Form is a common form that is unrelated to your database (model ).


1 Answers

If you can't use a third-party app and want to add a class (e.g., "form-control") to every field in a form in a DRY manner, you can do so in the form class __init__() method like so:

class ExampleForm(forms.Form):     # Your declared form fields here     ...      def __init__(self, *args, **kwargs):         super(ExampleForm, self).__init__(*args, **kwargs)         for visible in self.visible_fields():             visible.field.widget.attrs['class'] = 'form-control' 

You might need to handle checking for existing classes in attrs too, if for some reason you'll be adding classes both declaratively and within __init__(). The above code doesn't account for that case.

Worth mentioning:

You specified that you don't want to use third-party packages. However, I'll take one second to mention that one of the simplest ways of automatically making forms render in the style of Bootstrap is to use django-crispy-forms, like this:

# settings.py CRISPY_TEMPLATE_PACK = 'bootstrap3'  # forms.py from crispy_forms.helper import FormHelper class ExampleForm(forms.Form):     # Your declared form fields here     ...     helper = FormHelper()  # In your template, this renders the form Bootstrap-style: {% load crispy_forms_tags %} {% crispy form %} 
like image 154
Christian Abbott Avatar answered Oct 10 '22 19:10

Christian Abbott