Django集成xadmin后可以提供比较丰富的后台管理功能和好看的样式,但是它不支持富文本功能。本次基于在基于Django+xadmin的基础上,在集成DjangoUeditor后,就可以愉快的玩起富文本功能啦。
xadmin配置参照我的上一篇文章
DjangoUeditor安装
1、下载安装包文件
下载地址:https://github.com/twz915/DjangoUeditor3/
image.png
2、解压下载好的安装包,将DjangoUeditor文件复制到python安装环境的Lib\site-packages目录
image.pngimage.png
DjangoUeditor配置
1、在extra_apps目录下将上述DjangoUeditor文件复制一份放置其中
image.png2、在Setting中注册DjangoUeditor
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'DjangoUeditor',
'article',
'xadmin',
'crispy_forms',
]
image.png
配置富文本中使用的静态文件存储目录,在项目中新建一个media文件夹,存储富文本中放置的图片以及附件
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
image.png
3、models中配置富文本字段的使用
from django.db import models
# Create your models here.
from DjangoUeditor.models import UEditorField
class Article(models.Model):
title = models.CharField(max_length=100, verbose_name='标题')
brief=models.CharField(max_length=500, verbose_name='简介')
content = UEditorField(width=1200, height=400, toolbars="full", imagePath="images/", filePath="files/",upload_settings={"imageMaxSize":1204000},settings={}, verbose_name='内容')
create_time = models.DateTimeField(auto_now_add=True, verbose_name='发表时间')
class Meta:
db_table ='Article'
verbose_name ='文章'
verbose_name_plural = verbose_name
def __str__(self):
return self.title
4、adminx中配置需要显示为富文本的字段
style_fields = {"content": "ueditor"}
image.png
5、在xadmin\plugins目录创建一个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)
6配置urls
import xadmin
from django.conf.urls import url,include
from blog.settings import MEDIA_ROOT,STATIC_ROOT
from django.views import static
urlpatterns = [
url('^static/(?P<path>.*)$', static.serve, {'document_root': STATIC_ROOT}, name='static'),
url(r'^media/(?P<path>.*)$',static.serve,{"document_root":MEDIA_ROOT},name='media'),
# 富文本
url(r'^ueditor/', include('DjangoUeditor.urls')),
url(r'^',xadmin.site.urls),
]
上述配置完毕后,xadmin后台即可使用富文本功能,效果如下
image.png如果想要在自己写的前端页面,显示富文本内容,可以参照如下配置
{{ data.content }}为自己显示的字段,具体根据自己的来配置,我这里content 为富文本显示的字段
{% autoescape off %}
{{ data.content }}
{% endautoescape %}
{% autoescape off %}, {% endautoescape %}为关闭Django转义,否则页面显示为html源码
配置测试源码地址如下
链接:https://pan.baidu.com/s/1FQEXiQ3o6IEpWFyJ0htRtQ
提取码:ege2
复制这段内容后打开百度网盘手机App,操作更方便哦
网友评论