There are many options available for customizing the display and functionality of these built-in views. For more details on that, see Customizing Built-in Views. For more details on the other ORM backends that are available, see Using Different Database Backends.,Now, you’ll need to manually pass in some context variables for the Flask-Admin templates to render correctly when they’re being called from the Flask-Security views. Defining a security_context_processor function will take care of this for you:,There may be some scenarios where you want most of the built-in ModelView functionality, but you want to replace one of the default create, edit, or list views. For this you could override only the view in question, and all the links to it will still function as you would expect:,Introduction To Flask-Admin Getting Started Authorization & Permissions Customizing Built-in Views Grouping Views Adding Your Own Views Working With the Built-in Templates
from flask import Flask from flask_admin import Admin app = Flask(__name__) # set optional bootswatch theme app.config['FLASK_ADMIN_SWATCH'] = 'cerulean' admin = Admin(app, name = 'microblog', template_mode = 'bootstrap3') # Add administrative views here app.run()
from flask_admin.contrib.sqla import ModelView # Flask and Flask - SQLAlchemy initialization here admin = Admin(app, name = 'microblog', template_mode = 'bootstrap3') admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(Post, db.session))
{% extends 'admin/master.html' %}
{% block body %}
<p>Hello world</p>
{% endblock %}
class MicroBlogModelView(sqla.ModelView): def is_accessible(self): return login.current_user.is_authenticated def inaccessible_callback(self, name, ** kwargs): # redirect to login page if user doesn 't have access return redirect(url_for('login', next = request.url))
{
% extends 'admin/master.html' %
}
def security_context_processor():
return dict(
admin_base_template = admin.base_template,
admin_view = admin.index_view,
h = admin_helpers,
)
We can do this by overwriting our view functions is_accessible method as below:
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1'):
return True
return False
For my second question we just need to give the endpoint as for our BaseView as below:
class MyView(BaseView):
@expose('/')
def index(self):
return self.render('views.html')
admin.add_view(MyView(name = 'Custom Views', endpoint = 'customviews'))
And then in your jinja template you need to call it:
href = "{{ url_for('customviews.index') }}
I know this is an old question, but for the following code
current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1')
What I like to do is having a hybrid_property
(available on both Peewee and SQLAlchemy) inside my User class that consolidates these properties. So it'd look something like this:
@hybrid_property
def user_has_administrative_rights(self):
return self.has_role('superuser') or self.has_role('user')
Simple and extensible admin interface framework for Flask,The biggest feature of Flask-Admin is flexibility. It aims to provide a set of simple tools that can be used for building admin interfaces of any complexity. So, to start off with you can create a very simple application in no time, with auto-generated CRUD-views for each of your models. But then you can go further and customize those views & forms as the need arises.,Flask-Admin is extensively documented, you can find all of the documentation at https://flask-admin.readthedocs.io/en/latest/.,Flask-Admin is a batteries-included, simple-to-use Flask extension that lets you add admin interfaces to Flask applications. It is inspired by the django-admin package, but implemented in such a way that the developer has total control of the look, feel and functionality of the resulting application.
To run the examples in your local environment:
1. Clone the repository::
git clone https: //github.com/flask-admin/flask-admin.git
cd flask - admin
2. Create and activate a virtual environment::
virtualenv env - p python3
source env / bin / activate
3. Install requirements::
pip install - r examples / sqla / requirements.txt
4. Run the application::
python examples / sqla / run_server.py
To build the docs in your local environment, from the project directory:
tox - e docs - html
To install Flask-Admin, simply:
pip install flask - admin
Or alternatively, you can download the repository and install manually by doing:
git clone git @github.com: flask - admin / flask - admin.git cd flask - admin python setup.py install
To run the tests, from the project directory, simply:
pip install - r requirements - dev.txt pytest
You should see output similar to:
............................................. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Ran 102 tests in 13.132 s OK
For all the tests to pass successfully, you’ll need Postgres & MongoDB to be running locally. For Postgres:
> psql postgres
CREATE DATABASE flask_admin_test;\
q
>
psql flask_admin_test
CREATE EXTENSION postgis;
CREATE EXTENSION hstore;
Flask-Admin is an active project, well-tested and production ready.,The biggest feature of Flask-Admin is flexibility. It aims to provide a set of simple tools that can be used for building admin interfaces of any complexity. So, to start off with you can create a very simple application in no time, with auto-generated CRUD-views for each of your models. But then you can go further and customize those views & forms as the need arises.,Flask-Admin is extensively documented, you can find all of the documentation at https://flask-admin.readthedocs.io/en/latest/.,The docs are auto-generated from the .rst files in the /doc folder. So if you come across any errors, or if you think of anything else that should be included, then please make the changes and submit them as a pull-request.
…
elds
better margin below inline - field cards, and above inline - field buttons
To run the examples in your local environment:
1. Clone the repository::
git clone https: //github.com/flask-admin/flask-admin.git
cd flask - admin
2. Create and activate a virtual environment::
virtualenv env - p python3
source env / bin / activate
3. Install requirements::
pip install - r examples / sqla / requirements.txt
4. Run the application::
python examples / sqla / run_server.py
To build the docs in your local environment, from the project directory:
tox - e docs - html
To install Flask-Admin, simply:
pip install flask - admin
To run the tests, from the project directory, simply:
pip install - r requirements - dev.txt pytest
You should see output similar to:
............................................. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Ran 102 tests in 13.132 s OK
November 14, 2016 | Tags: Flask, SQLAlchemy,
pip install flask - admin
from flask_admin
import Admin
from flask_admin.contrib.sqla
import ModelView
admin = Admin(votr, name = 'Dashboard')
admin.add_view(ModelView(Users, db.session))
from flask_admin.contrib.sqla
import ModelView
from flask
import session, redirect, url_for, request
class AdminView(ModelView):
def __init__(self, * args, ** kwargs):
super().__init__( * args, ** kwargs)
self.static_folder = 'static'
def is_accessible(self):
return session.get('user') == 'Administrator'
def inaccessible_callback(self, name, ** kwargs):
if not self.is_accessible():
return redirect(url_for('home', next = request.url))
return redirect(request.args.get('next') or url_for('home'))
<form method="post" action="{{ url_for('login', next=request.args.get('next')) }}">
from flask_admin import Admin from admin import AdminView votr = Flask(__name__) # load config from the config file we created earlier votr.config.from_object('config') # create the database db.init_app(votr) # db.create_all(app = votr) migrate = Migrate(votr, db, render_as_batch = True) admin = Admin(votr, name = 'Dashboard', index_view = AdminView(Topics, db.session, url = '/admin', endpoint = 'admin')) admin.add_view(AdminView(Users, db.session)) admin.add_view(AdminView(Polls, db.session)) admin.add_view(AdminView(Options, db.session))
The implementation of this feature will just require changes to views.py. First, we will start by importing rules from the Flask-Admin forms:,In this recipe, we will create some custom forms using the forms provided by Flask-Admin. Also, we will create a custom action using the custom form.,In this recipe, we will customize this form to allow administrators to update the password for any user.,In the last recipe, we saw that the edit form view for the User record update had no option to update the password for the user. The form looked like the following screenshot:
from flask.ext.admin.form...