Open jackadam1981 opened 5 years ago
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib.sqla import ModelView
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///your_database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'your_secret_key' # Required for session security
db = SQLAlchemy(app)
# Define your SQLAlchemy models
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
# Customized AdminIndexView
class MyAdminIndexView(AdminIndexView):
def __init__(self, **kwargs):
super(MyAdminIndexView, self).__init__(**kwargs)
# Flask-Admin setup with both url and index_view
admin = Admin(
app,
url='/admin', # Set the URL prefix for Flask-Admin
name='Admin Panel',
template_mode='bootstrap3',
index_view=MyAdminIndexView(name='Dashboard')
)
# Add your models to Flask-Admin
admin.add_view(ModelView(User, db.session))
# Route to create a new user (example)
@app.route('/create_user')
def create_user():
new_user = User(username='test_user')
db.session.add(new_user)
db.session.commit()
return 'User created successfully!'
if __name__ == '__main__':
app.run(debug=True)
initialize Admin cannot define url and index_view at the same time.
admin = Admin( app, url='/test', name='nav',
index_view=MyAdminIndexView(name='try'),
)
work great
admin = Admin( app,
url='/test',
) work great
admin = Admin( app, url='/test', name='nav', index_view=MyAdminIndexView(name='try'), template_mode='bootstrap3', )
the url No entry into force