Skip to content

Commit bbca4b4

Browse files
committed
Form submission related changes
1 parent 7f60326 commit bbca4b4

File tree

8 files changed

+134
-184
lines changed

8 files changed

+134
-184
lines changed

README.md

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ Use forms in Bootstrap modals with Flask.
55
### Description
66

77
Plain forms can be boring. Using them in modals is possible, but requires
8-
JavaScript. Normal form submission in modals has its own problems.
8+
JavaScript. Normal form submission in modals is problematic.
99

1010
This Flask extension eases the process of using forms in Bootstrap modals.
1111
Bootstrap versions 4 and 5 are supported. No JavaScript coding is required on
12-
your part. The JavaScript is handled behind the scenes and uses
13-
html-over-the-wire. You can code in pure Python - flashing messages and rendering
12+
your part. You can code in pure Python - flashing messages and rendering
1413
templates.
1514

1615
### Installation
@@ -62,7 +61,12 @@ pip install Flask-Modals
6261

6362
You only need to import the function `render_template_modal` in your `routes.py`
6463
file. Use it instead of `render_template` in the route handler for the page with
65-
the modal form. It takes an extra argument - `modal` (the modal `id`).
64+
the modal form. It takes an extra argument - `modal` (the modal `id` with a default
65+
of `modal-form`).
66+
<br>
67+
The extension works by submitting the modal form twice - first via
68+
ajax and then, if all validations pass, normally. When submiited via ajax, it
69+
passes a field '_ajax' with the form, which can be used as shown below.
6670

6771
Example route handler:
6872

@@ -72,89 +76,66 @@ from flask_modals import render_template_modal
7276
@app.route('/', methods=['GET', 'POST'])
7377
def index():
7478

75-
# Following code is standard in any application
79+
ajax = '_ajax' in request.form # Add this line
7680
form = LoginForm()
7781
if form.validate_on_submit():
7882
if form.username.data != 'test' or form.password.data != 'pass':
7983
flash('Invalid username or password', 'danger')
8084
return redirect(url_for('index'))
8185

86+
if ajax: # Add these
87+
return '' # two lines
8288
login_user(user, remember=form.remember_me.data)
8389

8490
flash('You have logged in!', 'success')
8591
return redirect(url_for('home'))
8692

87-
# Following line is new
88-
return render_template_modal('index.html', form=form, modal='modal-form')
93+
# Add this line
94+
return render_template_modal('index.html', form=form)
8995
```
9096

9197
### Other usage
9298

93-
1. If you want to redirect to the same page outside the modal, use Flask's
94-
`session` proxy:
99+
If you want to render a template and not redirect:
95100

96-
```Python
97-
@app.route('/', methods=['GET', 'POST'])
98-
def index():
99-
100-
flag = session.pop('flag', False)
101-
102-
form = LoginForm()
103-
if form.validate_on_submit():
104-
if form.username.data != 'test' or form.password.data != 'pass':
105-
flash('Invalid username or password', 'danger')
106-
return redirect(url_for('index'))
107-
108-
login_user(user, remember=form.remember_me.data)
109-
110-
flash('You have logged in!', 'success')
111-
session['flag'] = True
112-
return redirect(url_for('index'))
113-
114-
modal = None if flag else 'modal-form'
115-
return render_template_modal('index.html', form=form, modal=modal)
116-
```
117-
<br>
118-
2. If you want to render a template and not redirect:
101+
```Python
102+
@app.route('/', methods=['GET', 'POST'])
103+
def index():
119104

120-
```Python
121-
@app.route('/', methods=['GET', 'POST'])
122-
def index():
105+
ajax = '_ajax' in request.form
106+
form = LoginForm()
107+
if form.validate_on_submit():
108+
if form.username.data != 'test' or form.password.data != 'pass':
109+
flash('Invalid username or password', 'danger')
110+
return render_template_modal('index.html', form=form)
123111

124-
form = LoginForm()
125-
if form.validate_on_submit():
126-
if form.username.data != 'test' or form.password.data != 'pass':
127-
flash('Invalid username or password', 'danger')
128-
return render_template_modal('index.html', form=form, modal='modal-form')
112+
if ajax:
113+
return ''
114+
login_user(user, remember=form.remember_me.data)
129115

130-
login_user(user, remember=form.remember_me.data)
116+
flash('You have logged in!', 'success')
117+
return render_template_modal('index.html', form=form)
131118

132-
flash('You have logged in!', 'success')
133-
return render_template_modal('index.html', form=form, modal=None)
119+
return render_template_modal('index.html', form=form)
120+
```
121+
If the above looks verbose, you can use the `response` decorator and
122+
return a context dictionary, like so:
134123

135-
return render_template_modal('index.html', form=form, modal='modal-form')
136-
```
137-
If the above looks verbose, you can use the `response` decorator and
138-
return a context dictionary, like so:
124+
```Python
125+
from flask_modals import response
139126

140-
```Python
141-
from flask_modals import response
142-
143-
@app.route('/', methods=['GET', 'POST'])
144-
@response('index.html')
145-
def index():
146-
...
147-
...
148-
return {'form': form, 'modal': 'modal-form'}
149-
```
150-
<br>
127+
@app.route('/', methods=['GET', 'POST'])
128+
@response('index.html')
129+
def index():
130+
...
131+
...
132+
return {'form': form}
133+
```
134+
<br>
151135

152136
### Note
153137

154138
1. See the examples folder in the repo for more details.
155139

156140
2. The extension loads the NProgress js library to display a progress bar during
157141
form submission.
158-
159-
3. If you have custom javascript which has global constants, you need to wrap it
160-
in an IIFE. Otherwise, there could be redeclaration errors.

examples/bootstrap4/app/routes.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import redirect, url_for, flash, session, render_template
1+
from flask import redirect, url_for, flash, session, render_template, request
22
from flask_login import login_user, logout_user
33
from flask_modals import render_template_modal, response
44

@@ -12,21 +12,23 @@ def index():
1212
is present in a modal.
1313
1414
`render_template_modal` takes the modal id as an argument apart
15-
from the `render_template` arguments.
15+
from the `render_template` arguments. THe default is 'modal-form'.
1616
'''
17+
ajax = '_ajax' in request.form
1718
form = LoginForm()
1819
if form.validate_on_submit():
1920
if form.username.data != 'test' or form.password.data != 'pass':
2021
flash('Invalid username or password', 'danger')
2122
return redirect(url_for('index'))
2223

24+
if ajax:
25+
return ''
2326
login_user(user, remember=form.remember_me.data)
2427

2528
flash('You have logged in!', 'success')
2629
return redirect(url_for('home'))
2730

28-
return render_template_modal('index.html', title='Index page', form=form,
29-
modal='modal-form')
31+
return render_template_modal('index.html', title='Index page', form=form)
3032

3133

3234
# Use the following code if you want to redirect to the same page that
@@ -35,44 +37,45 @@ def index():
3537
# @app.route('/', methods=['GET', 'POST'])
3638
# def index():
3739

38-
# modal = session.pop('modal', 'modal-form')
40+
# ajax = '_ajax' in request.form
3941

4042
# form = LoginForm()
4143
# if form.validate_on_submit():
4244
# if form.username.data != 'test' or form.password.data != 'pass':
4345
# flash('Invalid username or password', 'danger')
4446
# return redirect(url_for('index'))
4547

48+
# if ajax:
49+
# return ''
4650
# login_user(user, remember=form.remember_me.data)
4751

4852
# flash('You have logged in!', 'success')
49-
# session['modal'] = None
5053
# return redirect(url_for('index'))
5154

52-
# return render_template_modal('index.html', title='Index page', form=form,
53-
# modal=modal)
55+
# return render_template_modal('index.html', title='Index page', form=form)
5456

5557
# Use the following code if you want to render a template instead of
5658
# redirecting.
5759
#
5860
# @app.route('/', methods=['GET', 'POST'])
5961
# def index():
6062

63+
# ajax = '_ajax' in request.form
6164
# form = LoginForm()
6265
# if form.validate_on_submit():
6366
# if form.username.data != 'test' or form.password.data != 'pass':
6467
# flash('Invalid username or password', 'danger')
6568
# return render_template_modal('index.html', title='Index page',
66-
# form=form, modal='modal-form')
67-
69+
# form=form)
70+
# if ajax:
71+
# return ''
6872
# login_user(user, remember=form.remember_me.data)
6973

7074
# flash('You have logged in!', 'success')
7175
# return render_template_modal('index.html', title='Index page',
72-
# form=form, modal=None)
76+
# form=form)
7377

74-
# return render_template_modal('index.html', title='Index page', form=form,
75-
# modal='modal-form')
78+
# return render_template_modal('index.html', title='Index page', form=form)
7679

7780
# Use the following code if you want to render a template instead of
7881
# redirecting and make the code less verbose.
@@ -81,19 +84,21 @@ def index():
8184
# @response('index.html')
8285
# def index():
8386

87+
# ajax = '_ajax' in request.form
8488
# form = LoginForm()
8589
# if form.validate_on_submit():
8690
# if form.username.data != 'test' or form.password.data != 'pass':
8791
# flash('Invalid username or password', 'danger')
88-
# return {'title': 'Index page', 'form': form,
89-
# 'modal': 'modal-form'}
92+
# return {'title': 'Index page', 'form': form}
9093

94+
# if ajax:
95+
# return ''
9196
# login_user(user, remember=form.remember_me.data)
9297

9398
# flash('You have logged in!', 'success')
94-
# return {'title': 'Index page', 'form': form, 'modal': None}
99+
# return {'title': 'Index page', 'form': form}
95100

96-
# return {'title': 'Index page', 'form': form, 'modal': 'modal-form'}
101+
# return {'title': 'Index page', 'form': form}
97102

98103

99104
@app.route('/home', methods=['GET', 'POST'])

0 commit comments

Comments
 (0)