标签:规则 http logs self toolbar course har 上传 edit
pip install djangoueditor
python setup.py install
直接安装到当前site-package中将djangoueditor添加到setting.py中
INSTALLED_APPS = [
...
‘DjangoUeditor‘,]
将URL添加到urlpatterns中去:
#富文本编辑器
url(r‘^ueditor/‘,include(‘DjangoUeditor.urls‘ )),
django自带的富文本编辑框为models.Textfield()
from DjangoUeditor.models import UEditorField
class Course(models.Model):
name = models.CharField(max_length=20, verbose_name='课程名称')
desc = models.TextField(verbose_name='课程描述')
detail = UEditorField(verbose_name='课程详情',width=600, height=300, toolbars="full", imagePath="course/ueditor/", filePath="course/ueditor/", upload_settings={"imageMaxSize":1204000},default='')
说明:
width,height :编辑器的宽度和高度,以像素为单位。imagePath :图片上传后保存的路径,如"images/",实现上传到"{{MEDIA_ROOT}}/images"文件夹。 注意:如果imagePath值只设置文件夹,则未尾要有"/" imagePath可以按python字符串格式化:如"images/%(basename)s_%(datetime)s.%(extname)s"。这样如果上传test.png,则文件会 被保存为"{{MEDIA_ROOT}}/images/test_20140625122399.png"。 filePath : 附件上传后保存的路径,设置规则与imagePath一样。更多用法
由于已经将xadmin源文件拷贝到了项目下,本文为extra_apps/xadmin,在xadmin下的plugin中新建一个ueditor.py文件,里面写入如下:
import xadmin
from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView
from DjangoUeditor.models import UEditorField
from DjangoUeditor.widgets import UEditorWidget
from django.conf import settings
class XadminUEditorWidget(UEditorWidget):
def init(self,**kwargs):
self.ueditor_options=kwargs
self.Media.js = None
super(XadminUEditorWidget,self).__init__(kwargs)
class UeditorPlugin(BaseAdminPlugin):
def get_field_style(self, attrs, db_field, style, **kwargs):
if style == 'ueditor':
if isinstance(db_field, UEditorField):
widget = db_field.formfield().widget
param = {}
param.update(widget.ueditor_settings)
param.update(widget.attrs)
return {'widget': XadminUEditorWidget(**param)}
return attrs
def block_extrahead(self, context, nodes):
js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js") #自己的静态目录
js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js") #自己的静态目录
nodes.append(js)
xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)
PLUGINS = (
...
‘ueditor‘,
)
class CourseAdmin(object):
...
style_fields = {"detail": "ueditor"}
浏览器为了web安全在对后端传来的html代码会进行转义,会将<>等符号进行转义,因此要对页面设置过滤器防止字符转义,
{% autoescape off %}
{{ course.detail }}
{% endautoescape %}
过滤器更多用法
django——xadmin中集成富文本编辑器ueditor
标签:规则 http logs self toolbar course har 上传 edit
原文地址:https://www.cnblogs.com/welan/p/9460721.html