I am learning Flask-Admin but I want to prevent users from going to the admin page. If you want to go to the admin page you must go to /a
where admin_state
becomes 2
. But when I do it I get redirect
again.Here is the code:
FLASK
from flask import Flask,redirect,url_for,render_template,request
from flask_sqlalchemy import SQLAlchemy
from flask_admin import BaseView,expose,Admin,AdminIndexView
from flask_admin.contrib.sqla import ModelView
from flask_login import UserMixin,login_user,logout_user,login_required,LoginManager,current_user
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'abc'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
admin_state = 1
class users(db.Model,UserMixin):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(200),unique=False,nullable=False)
password = db.Column(db.String(200),unique=False,nullable=False)
db.create_all()
class AdminHome(AdminIndexView):
def is_accessible(self):
if admin_state == 2:
return current_user.is_authenticated
def inaccessible_callback(self,name,**kwargs):
return redirect('/')
admin = Admin(app,name='Control',index_view=AdminHome())
@login_manager.user_loader
def user_loader(user_id):
return users.query.get(int(user_id))
@app.route('/a')
def home():
admin_state = 2
return 'asd'
@app.route('/')
def home1():
return 'asdasd'
if __name__ == '__main__':
app.run(debug=True)
I can’t understand why the admin page doesn’t allow it.
Is there any way to do it?
Thanks.