Skip to content

Commit da7cbbf

Browse files
authored
Refactor (#2)
1 parent 4ebc25c commit da7cbbf

39 files changed

+574
-134
lines changed

.github/assets/formify-demo.jpg

277 KB
Loading

README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
# README
1+
<div align="center">
2+
3+
<h1>Django Formify</h1>
4+
5+
<p>Django-Formify seamlessly integrates Tailwind CSS styles into your Django forms for a modern look.</p>
6+
7+
<p><strong><a href="https://django-formify.readthedocs.io/en/latest/">Documentation</a> &nbsp;|&nbsp; <a href="https://saashammer.com/lookbook/inspect/form_component/form_fields/">Demo site</a></strong></p>
8+
9+
<p><a href="https://pypi.org/project/django-formify/"><img src="https://badge.fury.io/py/django-formify.svg" alt="Pypi version"></a>
10+
<a href="https://github.com/rails-inspire-django/django-formify/actions/workflows/runtests.yml"><img src="https://github.com/rails-inspire-django/django-formify/actions/workflows/runtests.yml/badge.svg" alt="CI status"></a></p>
11+
12+
</div>
13+
14+
```html
15+
{% load formify %}
16+
17+
<form method="post">
18+
19+
{% csrf_token %}
20+
21+
{% render_form form %}
22+
23+
{% render_submit text='Hello Formify!' variant="primary" %}
24+
25+
</form>
26+
```
27+
28+
![Django Formify Demo](.github/assets/formify-demo.jpg)
29+
30+
## Documentation
31+
32+
## FAQ
33+
34+
### Django-Formify vs Crispy-Tailwind
35+

docs/source/formify_helper.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Formify Helper
2+
3+
## Workflow
4+
5+
Unlike other form rendering packages, below template tags
6+
7+
```bash
8+
{% render_form %}
9+
{% render_submit %}
10+
{% render_field %}
11+
{% render_form_errors %}
12+
```
13+
14+
Just accept arguments from the templates, and pass the arguments to the `formify_helper` to render them.
15+
16+
So core logic is in the `formify_helper`
17+
18+
The benefit of this approach is it can be easy to customize the logic.
19+
20+
## Global Formify Helper
21+
22+
The default formify helper is `django_formify.tailwind.formify_helper.FormifyHelper`
23+
24+
We can create a class to inherit and override some methods to change the rendering logic.
25+
26+
To change the default global formify helper, just add below code to your Django settings
27+
28+
```python
29+
FORMIFY = {
30+
"formify_helper": "xxxx",
31+
}
32+
```
33+
34+
Leveraging Python OOP, you can override some methods of the formify helper to customize the rendering behavior.
35+
36+
```bash
37+
{% render_form %} -> formify_helper.render_form
38+
{% render_submit %} -> formify_helper.render_submit
39+
{% render_field %} -> formify_helper.render_field
40+
{% render_form_errors %} -> formify_helper.render_form_errors
41+
```
42+
43+
## Field Dispatcher
44+
45+
To make logic of rendering field more clean, there is a field dispatcher in the `render_field` method.
46+
47+
For example, if a field is using `TextInput` widget, it will try to use below methods to render
48+
49+
```
50+
text_input
51+
fallback
52+
```
53+
54+
Notes:
55+
56+
1. If `text_input` method is not found in the `formify_helper` instance, `fallback` method will be used to render the field.
57+
2. This can help developers to control rendering behavior of the specific widgets.
58+
59+
If you have built some custom widgets, just add a method to the `formify_helper` and make the final result look well, this is much cleaner.
60+
61+
## Formify Helper Variables
62+
63+
Formify Helper have some variables such as:
64+
65+
```python
66+
form_show_errors = True
67+
form_show_labels = True
68+
field_wrapper_class = "field-wrapper mb-3"
69+
```
70+
71+
Developers can override or add more variables to change the behavior.
72+
73+
In the final Django html, just access the variable using ``{{ formify_helper.form_show_errors }}``
74+
75+
For example, to control if rendering field label or not
76+
77+
```html
78+
{% if field.label and formify_helper.form_show_labels %}
79+
80+
{% endif %}
81+
```
82+
83+
## Formify Helper in Form
84+
85+
You can also create formify helper for the form to override the global formify helper.
86+
87+
```python
88+
from django_formify.tailwind.formify_helper import FormifyHelper
89+
90+
class ExampleForm(forms.Form):
91+
92+
def __init__(self, *args, **kwargs):
93+
super().__init__(*args, **kwargs)
94+
self.formify_helper = FormifyHelper()
95+
self.formify_helper.field_wrapper_class = "another-field-wrapper"
96+
```

docs/source/get_started.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Get Started
2+
3+
## Simple Form Rendering
4+
5+
Let's assume you already have Django forms.
6+
7+
To render the form with good style in the template
8+
9+
```html
10+
{% load formify %}
11+
12+
<!DOCTYPE html>
13+
<html>
14+
<head>
15+
<title>My Form</title>
16+
<script src="https://cdn.tailwindcss.com?plugins=forms"></script>
17+
</head>
18+
<body>
19+
20+
<div class="w-full max-w-3xl mx-auto px-4">
21+
<form method="post">
22+
23+
{% csrf_token %}
24+
25+
{% render_form form %}
26+
27+
{% render_submit text='Hello Formify!' css_class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" %}
28+
29+
</form>
30+
</div>
31+
32+
</body>
33+
</html>
34+
```
35+
36+
Notes:
37+
38+
1. We `{% load formify %}` at the top to use relevant tags.
39+
2. We use `<script src="https://cdn.tailwindcss.com?plugins=forms"></script>` to import Tailwind CSS and the `form` plugin, this is for testing purpose only.
40+
3. `{% render_form form %}` is to iterate form fields and display all the fields with their labels and errors.
41+
4. `{% render_submit %}` is to help render submit button with custom text and CSS class.
42+
43+
![](./images/simple_form.jpg)
44+
45+
It will also help display form non-field errors and form field errors as well.
46+
47+
![](./images/simple_form_errors.jpg)
48+
49+
## Render Fields Manually
50+
51+
If you want more control of the form layout, you can render fields manually.
52+
53+
```html
54+
{% load formify %}
55+
56+
<!DOCTYPE html>
57+
<html>
58+
<head>
59+
<title>My Form</title>
60+
<script src="https://cdn.tailwindcss.com?plugins=forms"></script>
61+
</head>
62+
<body>
63+
64+
<div class="w-full max-w-3xl mx-auto px-4">
65+
<form method="post">
66+
67+
{% csrf_token %}
68+
69+
<div class="grid grid-cols-12 gap-3">
70+
<div class="col-span-12 md:col-span-6">
71+
{% render_field form.name %}
72+
</div>
73+
<div class="col-span-12 md:col-span-6">
74+
{% render_field form.email %}
75+
</div>
76+
</div>
77+
78+
{% render_submit text='Submit' css_class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" %}
79+
80+
</form>
81+
</div>
82+
83+
</body>
84+
</html>
85+
```
86+
87+
Notes:
88+
89+
You can use `{% render_field form.email %}` to render specific form field.

docs/source/getting_started.md

-57
This file was deleted.

docs/source/images/form_grid.jpg

219 KB
Loading
235 KB
Loading

docs/source/images/simple_form.jpg

290 KB
Loading
373 KB
Loading

docs/source/index.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
django-formify
22
=====================
33

4-
A set of tools to help simplify your Django templates.
4+
Django-Formify seamlessly integrates Tailwind CSS styles into your Django forms for a sleek, modern look.
55

66
Topics
77
------
88

99
.. toctree::
1010
:maxdepth: 2
1111

12-
getting_started.md
12+
install.md
13+
get_started.md
14+
formify_helper.md
15+
layout.md

docs/source/install.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Installation
2+
3+
```shell
4+
$ pip install django-formify
5+
```
6+
7+
Then add the app into `INSTALLED_APPS` in settings.py
8+
9+
```python
10+
INSTALLED_APPS = [
11+
...,
12+
'django_viewcomponent', # new
13+
'django_formify', # new
14+
]
15+
```
16+
17+
`django_formify` has some components build by `django_viewcomponent`, to make it work, please also update `TEMPLATES` by following the instruction below.
18+
19+
Modify `TEMPLATES` section of settings.py as follows:
20+
21+
1. Remove `'APP_DIRS': True,`
22+
2. add `loaders` to `OPTIONS` list and set it to following value:
23+
24+
```python
25+
TEMPLATES = [
26+
{
27+
...,
28+
'OPTIONS': {
29+
'context_processors': [
30+
...
31+
],
32+
'loaders':[(
33+
'django.template.loaders.cached.Loader', [
34+
'django.template.loaders.filesystem.Loader',
35+
'django.template.loaders.app_directories.Loader',
36+
'django_viewcomponent.loaders.ComponentLoader',
37+
]
38+
)],
39+
},
40+
},
41+
]
42+
```

0 commit comments

Comments
 (0)