N
N
nurzhannogerbek2017-05-18 20:09:19
Django
nurzhannogerbek, 2017-05-18 20:09:19

How to add the ability to upload images to django-ckeditor?

Hello! Help me to understand.
Installed the django-ckeditor app for the text field. I want to enable uploading images from the user's computer. The problem is that there is no button for downloading an image from a computer, it is also not possible to add (more precisely, nothing is entered) the url address of an image from the Internet. What did you miss?
1) Installed ckeditor and ckeditor_uploader in INSTALLED_APPS setting.py.
2) Ran the collectstatic command .
URLs.py:

urlpatterns = i18n_patterns(
    # Django Ckeditor
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
)
if settings.DEBUG:
   urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
   urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')
MEDIA_URL = '/media/'

CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_JQUERY_URL = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"
CKEDITOR_IMAGE_BACKEND = 'pillow'
CKEDITOR_UPLOAD_SLUGIFY_FILENAME = False
CKEDITOR_RESTRICT_BY_USER = True
CKEDITOR_BROWSE_SHOW_DIRS = True

models.py:
class Post(models.Model):
    content= RichTextUploadingField(_('Description'))

forms.py:
class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('description',)
        widgets = {
           'description': CKEditorUploadingWidget()
        }

views.py:
def post_add(request):
    data = dict()
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        if post_form.is_valid():
           post = post_form.save(commit=False)
           ***Some code***
           post.save()
           data['form_is_valid'] = True
           posts = Post.objects.all()
           context = {'posts ': posts }
           context.update(csrf(request))
           data['html_post'] = render_to_string('project/post_list.html', context)
        else:
            data['form_is_valid'] = False
    else:
        post_form = PostForm()
    context = {'post_form': post_form}
    data['html_post_form'] = render_to_string('project/post_add.html', context, request=request)
    return JsonResponse(data)

6) post_add.html:
{% load widget_tweaks %}

  {{ post_form.media }}

  <form method="post" action="">
      {% csrf_token %}

      {% for field in post_form %}
         {% render_field field class="form-control" %}
      {% endfor %}

      <button type="submit">Create</button>
    </form>

<script type="text/javascript" src="{% static "ckeditor/ckeditor-init.js" %}"></script>

<script type="text/javascript" src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script>

PS If you remove ckeditor_uploader from INSTALLED_APPS , then downloading via the Internet works. But how to add the ability to download from a computer?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RUNAMAN, 2017-05-19
@RUNAMAN

did you create the directory? is there access to it?
"your app "/media/images

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question