Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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)
{% extends 'admin/model/edit.html' %}
{% block tail %}
{{ super() }}
<script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script>
{% endblock %}
@guinunez

This comment has been minimized.

Copy link

@guinunez guinunez commented Aug 27, 2014

are you planning to add file/image upload functionality?

@iilxy

This comment has been minimized.

Copy link

@iilxy iilxy commented Aug 29, 2014

how to add filebrowser?

@fanne

This comment has been minimized.

Copy link

@fanne fanne commented Oct 15, 2015

how to add code?

@candidature

This comment has been minimized.

Copy link

@candidature candidature commented Apr 21, 2016

Adding more precise steps as it may help a novice like me.

Added this in my model.py

from flask_admin.contrib import sqla
from src.knowledge.form import CKTextAreaField

class Test(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.UnicodeText)

class TestAdmin(sqla.ModelView):
form_overrides = dict(text=CKTextAreaField)

create_template = 'edit.html'
edit_template = 'edit.html'

form.py

class CKTextAreaWidget(widgets.TextArea):
def call(self, field, *kwargs):
kwargs.setdefault('class
', 'ckeditor')
return super(CKTextAreaWidget, self).call(field,
*_kwargs)

class CKTextAreaField(TextAreaField):
widget = CKTextAreaWidget()

Also used this class in my form.py as follows:

class Question(Form):
question = CKTextAreaField('Question text')

Created edit.html with this text.

{% extends 'admin/model/edit.html' %}

{% block tail %}
{{ super() }}

<script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script>

{% endblock %}

Don't worry from where it inherits.

Added following to manage.py

from flask.ext.admin import Admin

admin = Admin(app)
admin.add_view(TestAdmin(Test, db.session))
db.create_all()

@bekab95

This comment has been minimized.

Copy link

@bekab95 bekab95 commented May 21, 2018

Hello,
How to Override Mongoengine EmbeddedDocument Fields with Ckeditor ?

class Service(Document):
services = ListField(EmbeddedDocumentField(EmbeddedService)

class EmbeddedService(EmbeddedDocument):
name = StringField()

I want to have name Field with ckeditor when I open flask admin and insert new Service Document

@bekab95

This comment has been minimized.

Copy link

@bekab95 bekab95 commented May 27, 2018

flask-admin/flask-admin#361 (comment)
I managed to get ckeditor in embededdocuments but it will not work until I save document and is there any way to reload ckeditor after adding new field before saving ?

@greyli

This comment has been minimized.

Copy link

@greyli greyli commented Feb 22, 2019

Check this answer for the simpler solution based on Flask-CKEditor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment