17👍
The trick to bootstrap fields is injecting the form-control
class into each field, and making sure each field lives inside a form-group
dom element. To inject that class into each one of your fields, you could so something like:
class FoodForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(FoodForm, self).__init__(*args, **kwargs)
for field in iter(self.fields):
self.fields[field].widget.attrs.update({
'class': 'form-control'
})
Now, in your django template you can iterate through your forms fields, placing each one into a bootstrap’d form-group
. For example:
<form method="POST" class="post-form">
{% for field in form %}
<div class="form-group">
{{ field }}
</div>
{% endfor %}
</form>
I’ll also say that this setup (Django + Bootstrap) is super common now adays, so googling “Bootstrap forms with django” should yield a wealth of knowledge on customizing this even further. Good luck!
- [Django]-Showing auth_message's
- [Django]-Celery scheduled tasks with expiration time for each task instance?
- [Django]-Cannot type password. (creating a Django admin superuser)
- [Django]-Django get current view in template
- [Django]-Django ORM access optimization with Filter
Source:stackexchange.com