Django 集成DjangoUeditor 在图片文件路径错误问题

废话不多说

使用DjangoUeditor 富文本编辑器时,上传图片不显示问题

1、首先集成DjangoUeditor,我相信我大家应该都会集成。

2、在集成好以后,使用过程中发现,上传图片不显示。类似这样:

分析一下原因, 看到后台日志发现:

发现是我们在上传后,文件传到了路径中,但是不会正常显示。

这里,我直接说下,需要配置的地方:

1、settings.py 中,需要设置两个地方:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_DIRS = (
    os.path.join(BASE_DIR, 'media')
)

 如下图:

 下面这个,只需要找到TEMPLATES  加上最下面红框中的那句话即可。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',

                # 下面为添加
                'django.template.context_processors.media',  # 将media_url上传文件路径注册到模板中
            ],
        },
    },
]

如下图:

2、配置urls.py

先要导入需要的包文件:

from django.views.static import serve  # 上传文件处理函数
from .settings import MEDIA_ROOT  # 从配置中导入MEDIA_ROOT

 

url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT})

3、字段设置:models.py

case_details = UEditorField(width=1200, height=800, toolbars="full",
                                imagePath="images/",
                                filePath="files/",
                                upload_settings={"imageMaxSize": 1204000},
                                settings={},
                                verbose_name="案列详情",
                                null=True,
                                blank=True)

设置完成:

在打开DjangoUeditor 查看一下显示ok:

搞定收工 

猜你喜欢

转载自blog.csdn.net/u012798683/article/details/105462127