Django集合Ueditor

本文介绍如何在Python 3.6环境下手动安装DjangoUeditor富文本编辑器,并详细展示了从下载源码、配置环境到在Django项目中使用的全过程。包括设置安装步骤、settings.py和urls.py配置、Xadmin后台集成、model字段定义及前端页面展示等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语言版本环境:python3.6

1、win安装步骤:

1 git下载源码https://github.com/zhangfisher/DjangoUeditor

2 解压DjangoUeditor3-master.tar

3 cd C:\Users\fj\Desktop\DjangoUeditor3-master

4 python setup.py install 

官方建议使用pip install DjangoUeditor ,但是我使用之后报错。故自己下载安装包,手动安装。大家可以先按官方建议。报错在进行手动安装

2、settins.py配置

INSTALLED_APPS中加入'DjangoUeditor'

3、urls.py配置

urlpatterns中加入url(r'^ueditor/',include('DjangoUeditor.urls' )),

4、创建ueditor文件到plugins(xadmin后台)

 1 import xadmin
 2 from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView
 3 from DjangoUeditor.models import UEditorField
 4 from DjangoUeditor.widgets import UEditorWidget
 5 from django.conf import settings
 6 
 7 
 8 class XadminUEditorWidget(UEditorWidget):
 9     def __init__(self,**kwargs):
10         self.ueditor_options=kwargs
11         self.Media.js = None
12         super(XadminUEditorWidget,self).__init__(kwargs)
13 
14 class UeditorPlugin(BaseAdminPlugin):
15 
16     def get_field_style(self, attrs, db_field, style, **kwargs):
17         if style == 'ueditor':
18             if isinstance(db_field, UEditorField):
19                 widget = db_field.formfield().widget
20                 param = {}
21                 param.update(widget.ueditor_settings)
22                 param.update(widget.attrs)
23                 return {'widget': XadminUEditorWidget(**param)}
24         return attrs
25 
26     def block_extrahead(self, context, nodes):
27         js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js")         #自己的静态目录
28         js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js")   #自己的静态目录
29         nodes.append(js)
30 
31 xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)#修改页面
32 xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)#新增页面

5、model修改内容

1 from DjangoUeditor.models import UEditorField
2 class Course(models.Model):
3     desc = models.CharField(max_length=300,verbose_name=u'课程描述')
4     #imagePath  图片存储路径
5     detail = UEditorField(verbose_name = u'课程详情', width=600, height=300,  imagePath="courses/ueditor/",
6                           filePath="courses/ueditor/", default='')

 7、配置xadmin/plugins中的init文件(必须添加,否则无法生效)

 在PLUGINS里面添加'ueditor'

8、xadmin添加style_fields

 1 from .models import Course
 2 class CourseAdmin(object):
 3     list_display = ['name', 'desc', 'detail', 'degree','learn_times','studens','click_num','get_zj_nums','go_to']#后台显示哪些列
 4     search_fields = ['name', 'desc', 'detail', 'degree','studens']# 搜索,搜索中不能添加时间比较麻烦,放在过滤里面
 5     list_filter = ['name', 'desc', 'detail', 'degree','learn_times','studens']#过滤
 6     ordering = ('-click_num',)#显示排序
 7     readonly_fields = ['click_num']#只读 后台不可编辑
 8     exclude = ['fav_numbers']#隐藏字段  此字段与readonly_fields互斥
 9     inlines = [LessonInLine,CoursesResourceInLine]
10     list_editable = ['degree','desc']#在后台列表页面有哪些字段可以修改
11     refresh_times = [3,5] #对列表页定时刷新,3和5分别代表秒
12     style_fields = {"detail":"ueditor"}#指明某个字段要使用ueditor
13 xadmin.site.register(Course,CourseAdmin)

9、前端页面调用

1 {% autoescape off %}#关闭转义

2 {{ course.detail }}

3 {% endautoescape %} 

 

转载于:https://www.cnblogs.com/xb88/p/8213491.html

Django-ueditor是一个用于将百度富文本编辑器(UEditor)集成到Django Web应用中的第三方库。以下是基本步骤来安装和使用它: 1. **安装**: 使用pip安装: ``` pip install django-ueditor ``` 2. **配置**: - 在`settings.py`中添加到INSTALLED_APPS列表中,并设置静态文件目录路径: ```python INSTALLED_APPS = [..., 'django_ueditor'] STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] ``` - 在`settings.py`中配置ueditor的配置项,例如服务器地址、文件保存路径等。 3. **模型字段**: 在需要富文本编辑的模型中,添加`ckeditor_uploader`字段: ```python from django.db import models from django_ckeditor_uploads.fields import RichTextUploadingField class MyModel(models.Model): content = RichTextUploadingField() ``` 4. **视图和模板**: - 在视图函数中,实例化Ueditor对象并返回响应: ```python from django.shortcuts import render from django.core.files.storage import FileSystemStorage from django_ueditor.widgets import UEditorWidget def my_view(request): # 创建文件存储对象 storage = FileSystemStorage(location='media/ueditor/') # 初始化ueditor widget ueditor_widget = UEditorWidget(storage=storage) context = {'content': ueditor_widget} return render(request, 'my_template.html', context) ``` 5. **模板**: 在HTML模板中,引入ueditor.js和样式文件,然后使用`ckeditor`标签插入富文本编辑区域: ```html {% load static %} <script type="text/javascript" src="{% static 'ueditor/ueditor.config.js' %}"></script> <script type="text/javascript" src="{% static 'ueditor/ueditor.all.min.js' %}"></script> <script id="id_content" name="content"> <!-- 这里由前端框架填充 --> </script> <!-- ...其他内容... --> <textarea id="id_my_model_content" rows="10" cols="80">{% csrf_token %}{{ content|safe }}</textarea> ``` 6. **上传处理**: 需要创建一个处理文件上传的URL,通常与`MEDIA_URL`关联,并在ueditor配置中设置。 **
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值