安装django-xadmin:
官方下载:https://xadmin.readthedocs.io/en/docs-chinese/
安装xadmin:pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
xadmin相对于admin,将后台用户界面增加了更多功能,而且界面也会更美观。所以以前admin的一些配置也需要做些改变
1.在settings.py中注册
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘xadmin‘,
‘crispy_forms‘,
‘reversion‘,
]
2.将以前的admin修改为adminx 然后引入xadmin
import xadmin
from xadmin import views
3.可以用xadmin里内置好了的配置:
enable_themes = True #切换xadmin主题
use_bootswatch = True #切换主题,和enable_themes一起用。
site_title = ‘在线教育后台管理系统‘ #设置页面标题
site_footer = ‘Powered by lcl -2020‘ #设置页脚
menu_style = ‘accordion‘ #设置菜单栏收缩
model_icon=‘图片的代码‘: #设置图片,图片都在http://www.fontawesome.com.cn/
解压文件,将DjangoUeditor文件放到主文件下。
修改源码:虚拟环境====》Lib====》site-packages====》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)
在xadmin/plugins/init.py里添加ueditor插件。(注意逗号)
PLUGINS = (
‘ueditor‘,
)
在adminx.py里使用
在需要用的类下。
#address就是要显示为富文本的字段名
style_fields = {"address": "ueditor"}
在settings里注册DjangoUeditor
INSTALLED_APPS = [
‘DjangoUeditor‘,
]
原文:https://www.cnblogs.com/nihao2/p/13922389.html