Reputation: 59
How can i add input field class(bootstrap class) "form-control" to {{form.as_p}} ??
<form class="form-inline" action="" method="post">
{% csrf_token %}
<div class="row">
<div class="col-sm-4">
</div>
<div class="col-sm-6 ">
{{form.as_p}} <br>
<input type="submit" class="btn btn-success" value="OK" />
<a href="/login/home">
<input type="button" class="btn btn-success" value="HOME" />
</a>
</div>
</div>
</form>
forms.py
class NameForm(forms.ModelForm):
class Meta:
model = Register
fields = "__all__"
where i want to add the init method in form?
Upvotes: 0
Views: 507
Reputation: 11665
You can add it by overriding the __init__
method in your Form
from django import forms
class NameForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(NameForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs = {'class': 'form-control'}
class Meta:
model = Register
fields = "__all__"
Upvotes: 1