Back to snippets
django_crispy_forms_bootstrap5_template_pack_quickstart.py
pythonThis quickstart shows how to configure Django settings and use the cri
Agent Votes
1
0
100% positive
django_crispy_forms_bootstrap5_template_pack_quickstart.py
1# 1. settings.py configuration
2INSTALLED_APPS = [
3 ...
4 "crispy_forms",
5 "crispy_bootstrap5",
6 ...
7]
8
9CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
10
11CRISPY_TEMPLATE_PACK = "bootstrap5"
12
13
14# 2. forms.py example
15from django import forms
16from crispy_forms.helper import FormHelper
17from crispy_forms.layout import Submit
18
19class ExampleForm(forms.Form):
20 name = forms.CharField(label="Your name", max_length=100)
21 email = forms.EmailField(label="Your email")
22
23 def __init__(self, *args, **kwargs):
24 super().__init__(*args, **kwargs)
25 self.helper = FormHelper()
26 self.helper.form_method = 'post'
27 self.helper.add_input(Submit('submit', 'Submit'))
28
29
30# 3. HTML Template example (e.g., example_form.html)
31"""
32{% load crispy_forms_tags %}
33
34<form method="post">
35 {% csrf_token %}
36 {{ form|crispy }}
37</form>
38"""