Django + xadmin +Ueditor

 一,下载ueditorde js文件,放到static文件下,

  

二,配置setting,

INSTALLED_APPS = [
  'DjangoUeditor',
  'crispy_forms',

]
STATIC_URL = '/static/'
MEDIA_ROOT = BASE_DIR + '/static/upload/upimg/'
MEDIA_URL = '/static/upload/upimg/'
STATICFILES_DIRS = [
'static',
BASE_DIR + '/static/'

]

三,配置url

from django.conf.urls import url,include
url(r'^ueditor/', include('DjangoUeditor.urls')),

四,在adminx.py里配置

class ModuleInterDisplay(object):
style_fields = {"content": 'ueditor'}
def get_field_style(self, db_field, style, **kwargs):
if style in ('ueditor',):
attrs = {'widget': Ueditor} # 此处是顶部的widget引用
return attrs

五,建一个rte文件夹:里面包含__init__.py 和widgets.py,在widgets.py里放:

__author__='Administrator'
#coding=utf-8
from django import forms
from django.conf import settings
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe



class Ueditor(forms.Textarea):
def __init__(self, attrs={}):
super(Ueditor, self).__init__(attrs)

def render(self, name, value, attrs=None):
# rendered = super(Ueditor,self).render(name,value,attrs)
context = {
'name': name,
'STATIC_URL': settings.STATIC_URL,
'value': value,
}

return mark_safe(render_to_string('widgets/ueditor.html', context))

六,在templates下建一个widgets文件夹,下放一个ueditor.htnl文件:
{% load staticfiles %}

<link type="text/css" rel="stylesheet" href="{% static 'ueditor1261/themes/default/css/ueditor.css'%}" />
<script type="text/javascript" src="{% static 'ueditor1261/ueditor.all.js' %}" ></script>
<script type="text/javascript" src="{% static 'ueditor1261/ueditor.config.js'%}" ></script>
<script type="text/plain" id="myEditor" name="{{ name }}">
{% autoescape off %}
{{ value|default_if_none:'' }}
{% endautoescape %}
</script>

<script type="text/javascript">

var editor = new UE.ui.Editor({});
editor.render("myEditor");
</script>






posted @ 2017-10-20 11:27  少年喜雨  阅读(267)  评论(0)    收藏  举报