Getting CKEditor to work with Flask Admin

后端 未结 2 448
北恋
北恋 2021-01-16 02:14

I\'m trying to make turn the Flask Admin text box into a CKEdit box, as described here. However, when I run it and go to the existing admin fields it doesn\'t show any chang

2条回答
  •  醉梦人生
    2021-01-16 02:26

    Here is a simple working example using in-memory SQLite. There are only two files, the flask application and the edit Jinja2 html template.

    Library versions used are Flask 0.10.1, Flask-SQLAlchemy 2.1 and Flask-Admin 1.4.

    The flask application flask-ckeditor.py in the root folder:

    from flask import Flask
    from flask.ext.admin import Admin
    from flask.ext.admin.contrib.sqla import ModelView
    from flask.ext.admin.menu import MenuLink
    from flask.ext.sqlalchemy import SQLAlchemy
    from wtforms.widgets import TextArea, TextInput
    from wtforms.fields import TextAreaField
    
    app = Flask(__name__)
    
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
    app.config['SQLALCHEMY_ECHO'] = True
    app.config['DEBUG'] = True
    app.config['SECRET_KEY'] = 'super-secret'
    
    db = SQLAlchemy(app)
    
    
    class Test(db.Model):
        __tablename__ = 'tests'
        id = db.Column(db.Integer, primary_key=True)
        text = db.Column(db.UnicodeText)
    
    
    class CKEditorWidget(TextArea):
        def __call__(self, field, **kwargs):
            if kwargs.get('class'):
                kwargs['class'] += " ckeditor"
            else:
                kwargs.setdefault('class', 'ckeditor')
            return super(CKEditorWidget, self).__call__(field, **kwargs)
    
    
    class CKEditorField(TextAreaField):
        widget = CKEditorWidget()
    
    
    class TestAdminView(ModelView):
        form_overrides = dict(text=CKEditorField)
        can_view_details = True
        create_template = 'edit.html'
        edit_template = 'edit.html'
    
    
    @app.route('/')
    def index():
        return 'Click me to get to Admin!'
    
    # Create admin
    admin = Admin(app, name='Admin')
    admin.add_view(TestAdminView(model=Test, session=db.session, category='Tables', name='Test'))
    admin.add_link(MenuLink(name='Public Website', category='', url='/'))
    
    
    def build_db():
    
        tests = [
            {
                'text': "

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut

    " }, { 'text': "

    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque

    " }, { 'text': "

    At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium

    " } ] db.drop_all() db.create_all() for test in tests: test_db = Test(**test) db.session.add(test_db) db.session.commit() @app.before_first_request def create_user(): build_db() if __name__ == '__main__': app.run(debug=True)

    The Jinja2 html template file templates/edit.html:

    {% extends 'admin/model/edit.html' %}
    
    {% block tail %}
        {{ super() }}
        
    {% endblock %}
    

提交回复
热议问题