Created
March 18, 2013 19:04
-
-
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
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 %} |
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
pallets-eco/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 ?
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
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)
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 %}
<script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script>{{ super() }}
{% 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()