-
-
Save xstpl/acdbd3f6d01ad2faac36 to your computer and use it in GitHub Desktop.
Flask-Admin and CKEditor WYSIWYG textarea integration. Basically, all you have to do:
1. Create new wtforms widget which will emit 'ckeditor' class
2. Make new wtforms field which will use this widget
3. Create new jinja2 template, which includes ckeditor javascript
4. Tell flask-admin to use new field and new template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.sqlalchemy import SQLAlchemy | |
from flask.ext import admin, wtf | |
from flask.ext.admin.contrib import sqlamodel | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = '123456790' | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite' | |
db = SQLAlchemy(app) | |
class CKTextAreaWidget(wtf.TextArea): | |
def __call__(self, field, **kwargs): | |
kwargs.setdefault('class_', 'ckeditor') | |
return super(CKTextAreaWidget, self).__call__(field, **kwargs) | |
class CKTextAreaField(wtf.TextAreaField): | |
widget = CKTextAreaWidget() | |
class Test(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
text = db.Column(db.UnicodeText) | |
class TestAdmin(sqlamodel.ModelView): | |
form_overrides = dict(text=CKTextAreaField) | |
create_template = 'edit.html' | |
edit_template = 'edit.html' | |
if __name__ == '__main__': | |
admin = admin.Admin(app) | |
admin.add_view(TestAdmin(Test, db.session)) | |
db.create_all() | |
app.debug = True | |
app.run('0.0.0.0', 8000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'admin/model/edit.html' %} | |
{% block tail %} | |
{{ super() }} | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Thanks for sharing this code, I used it in one of my projects, the only problem I found is the following line on edit.html:
<script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script>
Since I am using https, the editor didn't show up, i fixed by using:
<script src="//cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script>
Hope you don't mind my comment, I really appreciate the fact that you shared this code!