廢話不多說
使用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:
搞定收工