Skip to content

Instantly share code, notes, and snippets.

@mrjoes
Created March 18, 2013 19:04
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save mrjoes/5189850 to your computer and use it in GitHub Desktop.
Save mrjoes/5189850 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
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
Copy link

are you planning to add file/image upload functionality?

@iilxy
Copy link

iilxy commented Aug 29, 2014

how to add filebrowser?

@fanne
Copy link

fanne commented Oct 15, 2015

how to add code?

@candidature
Copy link

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
Copy link

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
Copy link

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
Copy link

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