django-ckeditor的动态存储路径

本文详细介绍了如何配置django-ckeditor的动态存储路径,包括在settings.py中的相关设置,以及my_utils.py和主urls.py的使用,旨在解决django-ckeditor文件上传路径问题。适合django-ckeditor使用者参考。

django-ckeditor的动态存储路径

在安装好最新的django-ckeditor之后,关于django-ckeditor的简单配置我就不在赘述,网上已经有很多人写过了。详情也可以参考PyPI中的官方文档。

这篇文章主要讲解django-ckeditor的动态存储路径,也就是路径生成器的配置与用法。我在这里花了很多的时间,而网上似乎很难搜到现成完整的配置过程和方法,特此在这里记录并分享。

首先,要使用django-ckeditor的文件上传功能需要在settings.py中安装应用

INSTALLED_APPS = [
	# django's apps

    # my apps

    # third party applications
    'ckeditor',
    'ckeditor_uploader',	# 需要加入这个应用才支持文件的上传
]

settings.py中添加相关的配置

# ckeditor的配置

# 文件上传路径(在media/下),如果采用django的默认文件存储,则必须配置
# MEDIA_ROOT和MEDIA_URL,ckeditor的默认上传路径以MEDIA_ROOT为参考,
# 在MEDIA_ROO
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配置中设置。 **
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值