Crispy Forms
Forms
Installation of crisply and bootstrap
poetry add django-crispy-forms crispy-bootstrap5
Update settings.py
= [
INSTALLED_APPS # Other installed apps
'crispy_forms',
'crispy_bootstrap5',
]
# Set the Crispy Forms template pack to Bootstrap 5
= "bootstrap5"
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5" CRISPY_TEMPLATE_PACK
Form Layouts
Django Crispy Forms provides a powerful layout system that allows you to control the form layout directly in your form class.
Example
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Row, Column, Submit
class ExampleForm(forms.Form):
= forms.CharField()
first_name = forms.CharField()
last_name = forms.EmailField()
email = forms.CharField(widget=forms.Textarea)
message
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.layout = Layout(
Row('first_name', css_class='form-group col-md-6 mb-0'),
Column('last_name', css_class='form-group col-md-6 mb-0'),
Column(='form-row'
css_class
),'email',
'message',
'submit', 'Send Message', css_class='btn btn-primary')
Submit( )
- Layout Elements:
- Field: Wraps a form field. You can customize its attributes (e.g., CSS classes, placeholders).
- Row and Column: Define grid layouts for your form fields (e.g., using Bootstrap’s grid system).
- Submit: Adds a submit button to the form.
Helper Class
The FormHelper class in Django Crispy Forms is where most of the customization happens.
- Common Attributes:
- form_method: Specifies the form submission method (‘post’ or ‘get’).
- form_action: The URL where the form should be submitted.
- form_class: CSS classes for the form.
- layout: Defines the form layout using Layout, Field, Row, Column, etc.
Example
from crispy_forms.helper import FormHelper
class ExampleForm(forms.Form):
...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'example-form-id'
self.helper.form_class = 'form-horizontal'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_form'
Update Your Forms
from django import forms
from .models import ToDo
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class ToDoForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('Add todo', 'Add todo'))
class Meta:
= ToDo
model = ['title', 'description', 'completed']
fields = {
widgets 'description': forms.Textarea(attrs={'rows': 4}),
}
Update Views
@login_required
def add_todo(request):
if request.method == 'POST':
= ToDoForm(request.POST)
form if form.is_valid():
= form.save(commit=False)
todo = request.user
todo.user
todo.save()return redirect('list_todo') # Redirect to your todo list view
else:
# Add this to check for form validation errors
print(form.errors)
else:
= ToDoForm()
form
return render(request, 'todos/add_todo.html', {'form': form})
Update html
{% extends 'base.html' %}
{% block title %}Add New ToDo{% endblock %}
{% block content %}<h1>Add a New ToDo</h1>
<form method="post">
{% csrf_token %}
{% load crispy_forms_tags %}
{% crispy form %}
</form>
{% endblock %}
Template
base.html
nav.html
app
- app_specific.html
app2
- app2_specific.html