Giter Club home page Giter Club logo

bootstrap-flask's Introduction

Bootstrap-Flask

Added Django support

for django:

file: MyApp/jinja2.py

from django.templatetags.static import static
from django.urls import reverse
from django.conf import settings
from jinja2 import Environment
from django_bootstrap import Bootstrap5


def environment(**options):
    env = Environment(**options)
    env.add_extension("jinja2.ext.do") # add ext.do extension
    bootstrap = Bootstrap5(**settings.get("BOOTSTRAP_CONFIG", {})) # optional bootstrap setting from django conf
    env.globals.update(bootstrap.get_jinja_env()) # update jinja2 globals
    env.globals.update(
        {
            "static": static,
            "url": reverse,
            'settings': settings,

        }
    )
    return env

file: MyApp/settings.py

TEMPLATES = [
    {
        "BACKEND": 'django.template.backends.jinja2.Jinja2',
        "APP_DIRS": True,
        "DIRS": [BASE_DIR / 'django_bootstrap' / 'templates'], # <- add bootstrap templates directory 
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
            "environment": "MyApp.jinja2.environment"

        },
    }
  ]

PyPI - License Current version on PyPI Build status Coverage Status Open Collective

Bootstrap-Flask is a collection of Jinja macros for Bootstrap 4 & 5 and Flask. It helps you to render Flask-related data and objects to Bootstrap markup HTML more easily:

  • Render Flask-WTF/WTForms form object to Bootstrap Form.
  • Render data objects (dict or class objects) to Bootstrap Table.
  • Render Flask-SQLAlchemy Pagination object to Bootstrap Pagination.
  • etc.

Installation

$ pip install -U bootstrap-flask

Example

Register the extension:

from flask import Flask
# To follow the naming rule of Flask extension, although
# this project's name is Bootstrap-Flask, the actual package
# installed is named `flask_bootstrap`.
from flask_bootstrap import Bootstrap5

app = Flask(__name__)
bootstrap = Bootstrap5(app)

Assuming you have a Flask-WTF form like this:

class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
    password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)])
    remember = BooleanField('Remember me')
    submit = SubmitField()

Now with the render_form macro:

{% from 'bootstrap5/form.html' import render_form %}
<html>
<head>
<!-- Bootstrap CSS -->
</head>
<body>

<h2>Login</h2>
{{ render_form(form) }}

<!-- Bootstrap JS -->
</body>
</html>

You will get a form like this with only one line code (i.e. {{ render_form(form) }}):

form rendering

When the validation fails, the error messages will be rendered with proper style:

error form rendering

Read the Basic Usage docs for more details.

Live demo

Live demos of the latest release are:

Donate

If you find Bootstrap-Flask useful, please consider donating today. Your donation keeps Bootstrap-Flask maintained and updated with Bootstrap.

Links

Notes for Bootstrap 4 & 5 support

The Bootstrap 5 support is added in Bootstrap-Flask 2.0 version. Now you can use the separate extension class for different Bootstrap major versions.

For Bootstrap 4, use the Bootstrap4 class:

from flask_bootstrap import Bootstrap4

# ...
bootstrap = Bootstrap4(app)

and import macros from the template path bootstrap4/:

{% from 'bootstrap4/form.html' import render_form %}

For Bootstrap 5, use the Bootstrap5 class:

from flask_bootstrap import Bootstrap5

# ...
bootstrap = Bootstrap5(app)

and import macros from the template path bootstrap5/:

{% from 'bootstrap5/form.html' import render_form %}

The Bootstrap class and bootstrap/ template path are deprecated since 2.0 and will be removed in 3.0.

Migration from Flask-Bootstrap

If you come from Flask-Bootstrap, check out this tutorial on how to migrate to this extension.

Contributing

For guidance on setting up a development environment and how to make a contribution to Bootstrap-Flask, see the development documentation and Flask's contributing guidelines.

License

This project is licensed under the MIT License (see the LICENSE file for details). Some macros were part of Flask-Bootstrap and were modified under the terms of its BSD License.

bootstrap-flask's People

Contributors

greyli avatar pandermusubi avatar yuxiaoy1 avatar reedjones avatar jugmac00 avatar michaelbukachi avatar titaniumhocker avatar demetriex avatar nickovs avatar caffeinatedmike avatar nuno-andre avatar meizhaohui avatar a2yp avatar valr avatar muxuezi avatar neil-s avatar mvhconsult avatar msoucy avatar guidoiaquinti avatar ogabrielluiz avatar etiennepelletier avatar e2jk avatar dosenpfand avatar davidlesieur avatar cthoyt avatar chug2k avatar burhanharoon avatar bennye avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.