Django-CKEditor won't render images

2019-07-12 17:16发布

问题:

I've installed Django-CKEditor and configurated it for development purposes.

Now I can edit text and save it to db as text field. But with inserting images I have major problems...

I can insert image and it seems to save properly to local host (to correct folder) but when rendering image to preview or text editor I get only broken image picture. Apparently I haven't configured settings.py correctly. Main items from settings.py:

MEDIA_ROOT = ''
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
CKEDITOR_UPLOAD_PATH = 'media/uploads/'
CKEDITOR_UPLOAD_PREFIX = 'media/uploads/'

Also tried similar configuration than django-ckeditor testproject has but faced similar broken rendering images.

I'm using windows environment for development. Django-ckeditor version is 4.0.2

Any ideas what is wrong or where to start solving this?

回答1:

try collecting all static files Use following in shell

python manage.py collectstatic

Look into this link : https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/



回答2:

Found the root cause.

After debugging it found out that django-ckeditor has problems with Windows back-slashes. First I made my own correction but finally also found out that this has been already reported and correction proposal also made but not yet merged to master branch:

Fixing trouble with backward slashes in Windows



回答3:

Use django-vckeditor - it is easier to install and configure. For files uploaded via FTP or existing thumbnails will be created automatically.



回答4:

I had correct urls but images were still not rendering until I remembered to mark the html as safe in the template (i.e. {{my_model.my_text_field|safe}})