TextField にdjango-ckeditorアプリを使用しようとしています。
パソコンから画像を読み込みたいのですが、読み込みボタンがありません。また、画像のURLを入力できません。どうしたの?この問題を解決する方法。ここでは、私がしたいことを見ることができます。
1) setting.pyにckeditor
andckeditor_uploader
を追加します。INSTALLED_APPS
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'))
フォーム.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>