@@ -5,12 +5,11 @@ Use forms in Bootstrap modals with Flask.
55### Description
66
77Plain 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
1010This Flask extension eases the process of using forms in Bootstrap modals.
1111Bootstrap 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
1413templates.
1514
1615### Installation
@@ -62,7 +61,12 @@ pip install Flask-Modals
6261
6362You only need to import the function `render_template_modal` in your `routes.py`
6463file . 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
6771Example route handler:
6872
@@ -72,89 +76,66 @@ from flask_modals import render_template_modal
7276@ app.route(' /' , methods = [' GET' , ' POST' ])
7377def 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
1541381 . See the examples folder in the repo for more details.
155139
1561402 . The extension loads the NProgress js library to display a progress bar during
157141form 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.
0 commit comments