Django3.2+Python3.9项目中应用百度DjangoUeditor图片上传功能的应注意的一个配置

Django3.2+Python3.9项目中应用百度DjangoUeditor具体步骤不用细说了,本文只说应用中所遇到的问题及处理方案。

【问题描述】

本项目中某个Model的content字段已设置为UEditorField字段,如下所示:

content = UEditorField(verbose_name='题目主干',width=800, height=240,
                toolbars="full", imagePath="upimg/%(datetime)s.%(extname)s", filePath="upfile/",
                upload_settings={"imageMaxSize": 12040000},
                settings={}, command=None, blank=True)

在前端内容编辑html模板中,本人不想用Model对应的Form传入模板生成富文本编辑框,于是就采用了以下方式:

第一,在模板HTML顶部引入配置文件:

<link rel="stylesheet" type="text/css" href="{% static 'ueditor/themes/default/css/ueditor.css' %}">
<script type="text/javascript" src="{% static 'ueditor/ueditor.config.js' %}"></script>
<script type="text/javascript" charset="gbk" src="{% static 'ueditor/ueditor.all.min.js' %}"></script>
<script type="text/javascript" src="{% static 'ueditor/lang/zh-cn/zh-cn.js' %}"></script>

第二步,在模板HTML中间内容区适当的位置放置显示富文本编辑框的script代码:

<div id="id_content_div" class="col-12">
   <label>内容细节:</label>
   <script id="eq_body" name="eq_body" type="text/plain"></script>
</div>

第三步,在模板HTML页脚用js载入生成富文本编辑框,代码如下:

<script type="text/javascript">
 var content = UE.getEditor( 'content', {
        autoHeightEnabled: true,
        autoFloatEnabled: true,
        initialFrameWidth: $("#id_content_div").width-20,//动态地让富文本框适应父DIV的宽度
        initialFrameHeight:240,        

    });
    content.ready(function(){

     });
</script>

以上配置完成后,运行网站项目,打开编辑页面,完美呈现了富文本编辑框。

可是问题来了:当光标进入富文本编辑框后,单张图上传按钮变成灰色,多图上传按钮打开后,提示“后端配置项没有正常加载,上传插件不能正常使用”。如下图 所示。

这可不符合项目需要了,得想办法解决图片上传问题。

【问题解决】

第一个方案:网上求助,结果:失败。

    根据提示,应为相关配置问题,网上搜了很多介绍,照着折腾了老半天,最终没有办法解决,宣告失败。

第二个方案:借助Django FORM 表单方式处理。

     先针对Model Article建一个Ueditor Form表单,代码如下:

    

from DjangoUeditor.forms import UEditorField, UEditorModelForm
from .models import ExaminationQuestions
from django.forms import ModelForm, forms
from django.forms.widgets import Textarea, FileInput, Input,DateInput, Select


class ContentForm(UEditorModelForm):
    class Meta:
        model = Articles
        fields = ('content',)

        labels = {
            "detail": "内容细节",

        }
        widgets = {
            "detail": Textarea(attrs={"rows": "3"}),
        }

在 addContent 函数中初始化 ContentForm 并在context中传入模板:       

ContentForm = ContentForm()
context = {    
    'ContentForm':ContentForm,
}

在HTML模板中以变量载入:

<div id="id_eq_body_div_1" class="col-12">
 {{ ContentForm }}
</div>

运行页面,发现这样载入的富文本框能正常上传图片。但这样的方式老觉得麻烦,而且项目中好几个应用都要用到富文本框,老这样总觉得不便捷,个人喜欢就在前端载入。

查看HTML源码发现,{{ ContentForm }}生成的富文本框有一个 serverUrl 属性,于是想着把这个属性给放到最开始的方式中scrip代码中试试,变成如下这样:

<script type="text/javascript">


 var content = UE.getEditor( 'content', {
        autoHeightEnabled: true,
        autoFloatEnabled: true,
        initialFrameWidth: $("#id_eq_body_div").width-20,
        initialFrameHeight:240,
        serverUrl: '/ueditor/controller/?imageMaxSize=12040000&imagePathFormat=upimg%2F%25%28datetime%29s.%25%28extname%29s&filePathFormat=upfile%2F',


    });
    content.ready(function(){

     });

</script>

这样设置后,屏蔽掉上面说到的contentForm 变量,刷新页面,哈哈,图片上传功能有了。这样一来,其他几个富文本编辑页面就增加如上这样的serverUrl属性设置即可了。就不用再去建什么对应Model的form表单了。

以上算是在Django3.2+Python3.9项目中应用百度DjangoUeditor的一点经验记录,希望能帮到遇到类似问题的同学们。

以下是基于 Ubuntu 20.04 操作系统的 nginx+uwsgi+docker+django4.0+python3.9+mysql8.0 部署步骤: 1. 首先安装必要的软件和依赖: ``` sudo apt-get update sudo apt-get install -y git python3-pip python3-dev python3-venv build-essential libssl-dev libffi-dev nginx docker.io docker-compose mysql-server ``` 2. 创建并激活 Python 虚拟环境: ``` python3 -m venv myprojectenv source myprojectenv/bin/activate ``` 3. 安装 Django 和 uWSGI: ``` pip install django==4.0 uwsgi ``` 4. 创建 Django 项目: ``` django-admin startproject myproject cd myproject ``` 5. 配置 Django 数据库设置: 打开 `myproject/settings.py` 文件,在 `DATABASES` 添加以下内容: ``` DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myprojectdb', 'USER': 'myprojectuser', 'PASSWORD': 'mypassword', 'HOST': 'localhost', 'PORT': '3306', } } ``` 6. 创建 MySQL 数据库和用户: 登录 MySQL: ``` sudo mysql ``` 创建数据库: ``` CREATE DATABASE myprojectdb CHARACTER SET UTF8; ``` 创建用户: ``` CREATE USER 'myprojectuser'@'localhost' IDENTIFIED BY 'mypassword'; ``` 授权用户访问数据库: ``` GRANT ALL PRIVILEGES ON myprojectdb.* TO 'myprojectuser'@'localhost'; ``` 刷新权限: ``` FLUSH PRIVILEGES; ``` 退出 MySQL: ``` exit ``` 7. 测试 Django 项目是否能够正常运行: ``` python manage.py runserver ``` 在浏览器访问 `http://localhost:8000`,如果能够正常显示 Django 的欢迎页面,说明 Django 项目已经成功搭建。 8. 配置 uWSGI: 创建 `myproject/uwsgi.ini` 文件,添加以下内容: ``` [uwsgi] socket = :8001 chdir = /path/to/myproject module = myproject.wsgi:application master = true pidfile = /tmp/myproject-master.pid processes = 4 threads = 2 vacuum = true max-requests = 1000 harakiri = 60 ``` 9. 启动 uWSGI: ``` uwsgi --ini myproject/uwsgi.ini ``` 10. 配置 nginx: 创建 `/etc/nginx/sites-available/myproject` 文件,添加以下内容: ``` server { listen 80; server_name myproject.com; access_log /var/log/nginx/myproject.access.log; error_log /var/log/nginx/myproject.error.log; client_max_body_size 20M; location /static/ { alias /path/to/myproject/static/; } location /media/ { alias /path/to/myproject/media/; } location / { uwsgi_pass 127.0.0.1:8001; include /etc/nginx/uwsgi_params; } } ``` 11. 创建软链接: ``` sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/ ``` 12. 测试 nginx 配置是否正确: ``` sudo nginx -t ``` 如果没有错误,重启 nginx: ``` sudo systemctl restart nginx ``` 13. 创建 Dockerfile: 在 Django 项目根目录下创建 `Dockerfile` 文件,添加以下内容: ``` FROM python:3.9 RUN apt-get update \ && apt-get install -y nginx \ && rm -rf /var/lib/apt/lists/* RUN pip install uwsgi COPY ./requirements.txt /app/requirements.txt RUN pip install -r /app/requirements.txt COPY . /app WORKDIR /app RUN python manage.py collectstatic --noinput COPY ./myproject-nginx.conf /etc/nginx/sites-available/myproject RUN ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/ RUN rm /etc/nginx/sites-enabled/default EXPOSE 80 CMD service nginx start && uwsgi --ini /app/uwsgi.ini ``` 14. 创建 docker-compose.yml 文件: 在 Django 项目根目录下创建 `docker-compose.yml` 文件,添加以下内容: ``` version: '3' services: web: build: . ports: - "80:80" depends_on: - db volumes: - ./static:/app/static - ./media:/app/media environment: - DB_HOST=db - DB_NAME=myprojectdb - DB_USER=myprojectuser - DB_PASSWORD=mypassword db: image: mysql:8.0 volumes: - db_data:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=rootpassword - MYSQL_DATABASE=myprojectdb - MYSQL_USER=myprojectuser - MYSQL_PASSWORD=mypassword volumes: db_data: ``` 15. 构建和运行 Docker 容器: ``` sudo docker-compose up --build ``` 16. 测试 Django 项目是否能够正常运行: 在浏览器访问 `http://localhost`,如果能够正常显示 Django 的欢迎页面,说明 Django 项目已经成功部署到 Docker 容器。 至此,nginx+uwsgi+docker+django4.0+python3.9+mysql8.0 部署完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值