Skip to main content

Modals for Flask

Use forms in Bootstrap modals with Flask.

Description

Plain forms can be boring. Using them in modals is possible, but requires JavaScript. Normal form submission in modals is problematic.

This Flask extension eases the process of using forms in Bootstrap modals. Bootstrap versions 4 and 5 are supported. No JavaScript coding is required on your part. You can code in pure Python - flashing messages and rendering templates.

Installation

pip install Flask-Modals

Setup

  1. Import the Modal class and instantiate it in your app.py file.

    from flask_modals import Modal
    
    app = Flask(__name__)
    modal = Modal(app)
    

    You will also need a secret key in the app config (not shown).

  2. Alternatively if you are using the application factory pattern:

    from flask_modals import Modal
    
    modal = Modal()
    
    def create_app():
        app = Flask(__name__)
        modal.init_app(app)
    

  3. Include the following in the head tag of your base template.

    {{ modals() }}
    

  4. Include the following in the modal body.

    <div class="modal-body">
    {{ modal_messages() }}
    <form method="post">
    ...
    

Basic usage

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

Example route handler:

from flask_modals import render_template_modal

@app.route('/', methods=['GET', 'POST'])
def index():

    ajax = '_ajax' in request.form  # Add this line
    form = LoginForm()
    if form.validate_on_submit():
        if form.username.data != 'test' or form.password.data != 'pass':
            flash('Invalid username or password', 'danger')
            return redirect(url_for('index'))

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

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

    # Add this line
    return render_template_modal('index.html', form=form)

Other usage

If you want to render a template and not redirect:

@app.route('/', methods=['GET', 'POST'])
def index():

    ajax = '_ajax' in request.form
    form = LoginForm()
    if form.validate_on_submit():
        if form.username.data != 'test' or form.password.data != 'pass':
            flash('Invalid username or password', 'danger')
            return render_template_modal('index.html', form=form)

        if ajax:
            return ''
        login_user(user, remember=form.remember_me.data)

        flash('You have logged in!', 'success')
        return render_template_modal('index.html', form=form)

    return render_template_modal('index.html', form=form)

If the above looks verbose, you can use the response decorator and return a context dictionary, like so:

from flask_modals import response

@app.route('/', methods=['GET', 'POST'])
@response('index.html')
def index():
    ...
    ...
    return {'form': form}

Note

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

  2. The extension loads the NProgress js library to display a progress bar during form submission.

Metadata

Release files for Flask-Modals 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Modals 0.5.1
File Size Uploaded
Flask-Modals-0.5.1.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Modals 0.5.1
File Interpreter ABI Platform
Flask_Modals-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / Flask-Modals-0.5.1.tar.gz

Download URL Flask-Modals-0.5.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4a25923b2b6f1a600666205e0d2ab3344031af4176e20fb708003a6f9c66241d
BLAKE2b-256 checksum
How to use checksums
29746d930d8bc8b22e896114f40834a2163685e5f68b7751bf4861ccd1263008
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.2

Release files / Flask_Modals-0.5.1-py3-none-any.whl

Download URL Flask_Modals-0.5.1-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4b6e0cda43938e73017b4dc27f0e8b77ff6f28147aae0212e67e0b02a63467d
BLAKE2b-256 checksum
How to use checksums
db41461105e41d5ab80c8b23057c5b4dca54c52447b161c708183920286eef7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page