Skip to content

Commit fefcd70

Browse files
committed
Django Crispy Forms
1 parent b476afc commit fefcd70

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
---
2+
title : Django Crispy Forms
3+
sidebar_label : Crispy Forms
4+
---
5+
6+
# Django Crispy Forms
7+
8+
<SubHeading>Learn how to enhance a Django project with Crispy Forms</SubHeading>
9+
10+
This page provides a step-by-step tutorial on **Integrating Crispy Forms in [Django](https://www.djangoproject.com/)**, from simple forms to large forms, and cover styling options like Bootstrap and Tailwind.
11+
12+
![Django Crispy Forms - Tutorial provided by AppSeed.](https://user-images.githubusercontent.com/51070104/268675023-54ea4ace-a8ad-442b-9b43-2ba12a6403ba.jpg)
13+
14+
> Let's get started.
15+
16+
17+
## ✅ Set Up Django
18+
19+
If you haven't already, create a Django project and app. You can use the following commands:
20+
21+
```bash
22+
django-admin startproject project_name
23+
cd project_name
24+
python manage.py startapp myapp
25+
```
26+
27+
28+
## ✅ Configure Crispy Forms
29+
30+
Install Crispy Forms using pip:
31+
32+
```bash
33+
pip install django-crispy-forms
34+
```
35+
36+
Add `'crispy_forms'` to your `INSTALLED_APPS` in your project's `settings.py`:
37+
38+
```python
39+
INSTALLED_APPS = [
40+
# ...
41+
'crispy_forms',
42+
]
43+
```
44+
45+
Configure Crispy Forms to use a Bootstrap or Tailwind CSS framework. In this tutorial, we'll use Bootstrap. Add this to your `settings.py`:
46+
47+
```python
48+
CRISPY_TEMPLATE_PACK = 'bootstrap4'
49+
```
50+
51+
52+
## ✅ Create a Simple Form
53+
54+
Let's create a simple form with two fields, "Name" and "Email." First, define the form in `forms.py` within your app:
55+
56+
```python
57+
# myapp/forms.py
58+
from django import forms
59+
from crispy_forms.helper import FormHelper
60+
from crispy_forms.layout import Layout, Submit, Row, Column
61+
62+
class SimpleForm(forms.Form):
63+
name = forms.CharField(label="Name")
64+
email = forms.EmailField(label="Email")
65+
66+
helper = FormHelper()
67+
helper.layout = Layout(
68+
Row(
69+
Column('name', css_class='col-md-6'),
70+
Column('email', css_class='col-md-6'),
71+
),
72+
Submit('submit', 'Submit', css_class='btn-primary')
73+
)
74+
```
75+
76+
In this example, we use the `FormHelper` and `Layout` from Crispy Forms to customize the form's appearance. We've arranged the fields in a two-column layout.
77+
78+
79+
## ✅ Create the Django View
80+
81+
Now, let's create a view that renders the form and a template to display it. In your `views.py`:
82+
83+
```python
84+
# myapp/views.py
85+
from django.shortcuts import render
86+
from .forms import SimpleForm
87+
88+
def simple_form_view(request):
89+
if request.method == 'POST':
90+
form = SimpleForm(request.POST)
91+
if form.is_valid():
92+
# Process form data
93+
# Redirect or render a response
94+
else:
95+
form = SimpleForm()
96+
97+
return render(request, 'simple_form.html', {'form': form})
98+
```
99+
100+
In the `simple_form_view`, we handle both GET and POST requests. On GET, we create an empty form, and on POST, we validate the form data.
101+
102+
103+
## ✅ Create a Template
104+
105+
Create a template to display the form. In your app's `templates` folder, create a file named `simple_form.html`:
106+
107+
```html
108+
<!-- myapp/templates/simple_form.html -->
109+
{% extends "base.html" %}
110+
111+
{% block content %}
112+
<h2>Simple Form</h2>
113+
<form method="post" class="mt-3">
114+
{% csrf_token %}
115+
{{ form|crispy }}
116+
</form>
117+
{% endblock %}
118+
```
119+
120+
121+
## ✅ Adding Routing
122+
123+
In your app's `urls.py`, define a URL pattern for the form view:
124+
125+
```python
126+
# myapp/urls.py
127+
from django.urls import path
128+
from . import views
129+
130+
urlpatterns = [
131+
path('simple-form/', views.simple_form_view, name='simple_form'),
132+
]
133+
```
134+
135+
136+
## ✅ Styling Options (Bootstrap and Tailwind)
137+
138+
With Crispy Forms configured, you can easily change the form's style to Bootstrap or Tailwind. To switch to Tailwind, modify your `settings.py`:
139+
140+
```python
141+
CRISPY_TEMPLATE_PACK = 'tailwind'
142+
```
143+
144+
Then, your forms will use the Tailwind CSS framework.
145+
146+
## Start the Project
147+
148+
Run the development server:
149+
150+
```bash
151+
python manage.py runserver
152+
```
153+
154+
Visit `http://127.0.0.1:8000/simple-form/` in your browser to see and submit the form.
155+
156+
157+
## ✅ In Summary
158+
159+
In this tutorial, you've learned how to integrate Crispy Forms into a Django project:
160+
161+
1. Installed and configured Crispy Forms.
162+
2. Created a simple form with customization using the `FormHelper` and `Layout`.
163+
3. Created a view and template to display and handle the form.
164+
4. Added URL patterns to access the form.
165+
5. Explored styling options with Bootstrap and Tailwind.
166+
167+
**Styling Options:**
168+
169+
- **Bootstrap**: Crispy Forms can style your forms using Bootstrap CSS classes. This provides a clean and responsive design.
170+
171+
- **Tailwind CSS**: Tailwind is an alternative styling option, which gives you more flexibility in terms of design customization.
172+
173+
**Limitations of Crispy Forms:**
174+
175+
- Crispy Forms may not handle complex form layouts or styles out-of-the-box. You may need to customize the layout further if your form has intricate designs.
176+
177+
- For very large and complex forms, it can be challenging to manage the layout and styling using Crispy Forms alone. In such cases, you may need to use custom CSS or additional frontend frameworks for better control.
178+
179+
Remember that the flexibility and ease of use of Crispy Forms can greatly simplify form rendering and styling in your Django project, especially for smaller to medium-sized forms.
180+
181+
182+
## ✅ Resources
183+
184+
- 👉 Access [AppSeed](https://appseed.us/) for more starters and support
185+
- 👉 [Deploy Projects on Aws, Azure and DO](https://www.docs.deploypro.dev/) via **DeployPRO**
186+
- 👉 Create landing pages with [Simpllo, an open-source site builder](https://www.simpllo.com/)
187+
- 👉 Build apps with [Django App Generator](https://app-generator.dev/django/) (free service)

0 commit comments

Comments
 (0)