-
-
Save mrjoes/5189850 to your computer and use it in GitHub Desktop.
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 %} |
how to add filebrowser?
how to add code?
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() }}
{% 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()
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.
are you planning to add file/image upload functionality?