pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.79k stars 1.57k forks source link

initialize Admin cannot define url and index_view at the same time. #1919

Open jackadam1981 opened 5 years ago

jackadam1981 commented 5 years ago

initialize Admin cannot define url and index_view at the same time.

admin = Admin( app, url='/test', name='nav',

index_view=MyAdminIndexView(name='try'),

template_mode='bootstrap3',

)

work great

admin = Admin( app,

url='/test',

name='nav',
index_view=MyAdminIndexView(name='try'),
template_mode='bootstrap3',

) work great

admin = Admin( app, url='/test', name='nav', index_view=MyAdminIndexView(name='try'), template_mode='bootstrap3', )

the url No entry into force

ljluestc commented 3 months 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)