加载图片在django-ckeditor中不起作用?

时间:2017-05-18 16:46:32

标签: python django python-3.x django-1.10 django-ckeditor

我想在我的TextField上使用 django-ckeditor 应用。

我想从计算机加载图片,但没有加载按钮。我也无法输入图片的网址。我做错了什么?如何解决这个问题。在下面你可以看到我想要的。

1)我将ckeditorckeditor_uploader添加到INSTALLED_APPS setting.py:

2)我运行collectstatic管理命令。

urls.py:

urlpatterns += [
   url(r'^upload/', login_required(views.upload), name='ckeditor_upload'),
   url(r'^browse/', never_cache(login_required(views.browse)), name='ckeditor_browse'),
]

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 = "media/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 = ('content',)
        widgets = {
           'content': 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)

post_add.html:

  {% load widget_tweaks %}

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

      {{ post_form.media }}

      {% 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>

2 个答案:

答案 0 :(得分:0)

您正在使用RichTextField,切换到models.py中的RichTextUploadingField

答案 1 :(得分:0)

你在post_add.html

中丢失了这个{{post_form.as_p}}

https://github.com/django-ckeditor/django-ckeditor 你应该在链接中阅读更多detil