0

我想在我的 TextField 中使用django-ckeditor应用程序。

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

1)我在setting.py中添加ckeditor和:ckeditor_uploaderINSTALLED_APPS

2)我运行collectstatic管理命令。

网址.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)

设置.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

模型.py:

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

表格.py:

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

视图.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>
4

2 回答 2

0

你在 post_add.html 中丢失了这个 {{ post_form.as_p }}

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

于 2017-08-06T10:40:30.817 回答
0

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

于 2017-05-19T03:01:48.067 回答