FlaskAdmin is a third - party extension for Flask that provides an easy - to - use interface for creating admin panels. It allows you to manage database models directly from the web interface without writing a lot of boilerplate code.
The main components of FlaskAdmin are:
Admin
class and attach it to your Flask application.First, make sure you have Flask and FlaskAdmin installed. You can install them using pip
:
pip install flask flask-admin
Here is a simple example of setting up a Flask application with FlaskAdmin:
from flask import Flask
from flask_admin import Admin
# Create a Flask application
app = Flask(__name__)
# Create an instance of the Admin class
admin = Admin(app, name='My Admin Panel', template_mode='bootstrap3')
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
In this code:
Flask
from flask
and Admin
from flask_admin
.app
.Admin
class and attach it to the Flask application. We also give the admin panel a name and specify the template mode.Let’s assume we are using SQLAlchemy for database management. First, install flask - sqlalchemy
:
pip install flask-sqlalchemy
Here is an example of adding a model to the admin panel:
from flask import Flask
from flask_admin import Admin
from flask_sqlalchemy import SQLAlchemy
from flask_admin.contrib.sqla import ModelView
# Create a Flask application
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize SQLAlchemy
db = SQLAlchemy(app)
# Define a model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
# Create an instance of the Admin class
admin = Admin(app, name='My Admin Panel', template_mode='bootstrap3')
# Add the model view to the admin panel
admin.add_view(ModelView(User, db.session))
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
In this code:
SQLAlchemy
from flask_sqlalchemy
and ModelView
from flask_admin.contrib.sqla
.User
model with id
, name
, and email
fields.Admin
instance, we add a ModelView
for the User
model to the admin panel.FlaskAdmin allows you to customize the appearance and behavior of the admin panel. Here is an example of customizing the ModelView
:
from flask import Flask
from flask_admin import Admin
from flask_sqlalchemy import SQLAlchemy
from flask_admin.contrib.sqla import ModelView
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
class CustomUserView(ModelView):
# Exclude the email column from the list view
column_exclude_list = ['email']
# Add a custom name for the model in the admin panel
name = 'Custom User'
admin = Admin(app, name='My Admin Panel', template_mode='bootstrap3')
admin.add_view(CustomUserView(User, db.session))
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
In this code, we create a custom ModelView
class CustomUserView
. We exclude the email
column from the list view and give the model a custom name in the admin panel.
FlaskAdmin is a powerful and flexible extension for creating admin panels in Flask applications. It simplifies the process of managing database models and provides a user - friendly interface. By understanding the core concepts, typical usage scenarios, and best practices, you can create effective admin panels for your web applications. However, it is important to be aware of the common pitfalls and take appropriate measures to avoid them.