Skip to main content

Use forms in Bootstrap modals with Flask > 2.3

Project description

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.

This extension is a minor variantion of flask-modals to work with Flask>2.3. The _app_ctx_stack variable was removed from Flask in version 2.3 so we replaced it by a call to the _get_current_object() method of current_app object

Installation

pip install Flask-Modals2

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_modals2-0.6.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_modals2-0.6.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_modals2-0.6.0.tar.gz.

File metadata

  • Download URL: flask_modals2-0.6.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for flask_modals2-0.6.0.tar.gz
Algorithm Hash digest
SHA256 a0663b5df68730f11f88977ed3625553e8af7c356c3859c466b3cc34c3e23993
MD5 435535412b664a53f3e509718c1f9ceb
BLAKE2b-256 eba4e36b204e35047016b4e6f035a2e9332ec461f0da57f99c865af499375d58

See more details on using hashes here.

File details

Details for the file flask_modals2-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: flask_modals2-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for flask_modals2-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2716843d75a45b18128dfd38a7649bd9e5d16a698c6650ff7783e55a61be8319
MD5 042e1dd2c5a5a3448b35cc71f3c616cd
BLAKE2b-256 e2b1bcf0cf5d3ed10822e3a4895412d32557dc72b69a734a5ea218c11b5c10a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page