我讨厌问那些在类似情况下已经解决的问题,但我从 3 天开始就生气了。
我使用 django 1.10 和 Jquery 3.1.1(已经在我的页面中加载了另一个工具)。并尝试让 django-ckeditor 5.1.1 在我的项目上工作。我已经运行了 ckeditor_demo 并且它工作正常,因此正确安装了包。
我已经关注了 git doc。并在 Stack 上花费了几个小时。
ckeditor 在我的settings.py
:
from __future__ import absolute_import
import tempfile
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'blog',
'CreateYourLaws',
'captcha',
'registration',
'ckeditor',
'ckeditor_uploader',
]
# Plenty of others Settings for other tools...
# Settings for ckeditors
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(tempfile.gettempdir(), 'ck_static')
MEDIA_ROOT = os.path.join(tempfile.gettempdir(), 'ck_media')
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'Basic',
},
}
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_IMAGE_BACKEND = "pillow"
我也已经尝试过设置:
CKEDITOR_JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'
它也不起作用。我更喜欢删除它,因为我已经在我的模板中加载了 jquery 3.1.1。
我也已经运行了manage.py collectstatic
命令(当然是在写完我的设置之后)。
我的urls.py
:
from django.conf.urls import url, include
from . import views
from django.conf import settings
from django.conf.urls.static import static
#... and few others
urlpatterns = [
url(r'^$', views.home, name='home'),
# ... many others
url(r'^article/(\d+)$', views.view_article, name='article'),
url(r'^ckeditor/', include('ckeditor_uploader.urls'),)
] + static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT
) + static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
我的模型:
from django.db import models
# Other imports...
from ckeditor.fields import RichTextField
class Reflection(models.Model):
title = models.CharField(max_length=150, blank=True, null=True)
# Some Attributes
class Meta:
abstract = True
#...
class Proposition(Reflection):
text = RichTextField()
# Other attributes
我的表格:
from django import forms
# other imports...
from CreateYourLaws.models import Proposition
from ckeditor.widgets import CKEditorWidget
class PropositionForm(forms.ModelForm):
class Meta:
model = Proposition
fields = ('title', 'text')
labels = {'title': ('Nommez votre proposition de loi'),
'text': ("votre proposition de loi")}
widgets = {
'text': CKEditorWidget()
}
我的观点:
@login_required
def view_article(request, id_article):
# A very good and interesting code...
if request.method == 'POST' and 'propform' in request.POST:
propform = PropositionForm(request.POST)
if propform.is_valid():
proptitle = propform.cleaned_data['title']
prop = propform.cleaned_data['text']
prp = Proposition.objects.create(text=prop,
title=proptitle,
autor=User,
content_object=Article)
prp.save()
propform = PropositionForm()
# A bit of code...
return render(request,
'main.html',
locals())
我的模板:
{% block script %}
<script type="text/javascript" src="{% static 'js/jstree.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-ui.min.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'js/jquery-ui.min.css' %}"></link>
<script type="text/javascript" src="{% static 'js/myJS_CYL.js' %}"></script>
<script type="text/javascript" src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script>
<script type="text/javascript" src="{% static "ckeditor/ckeditor-init.js" %}"></script>
<style type="text/css" media="screen">
nav{display: block;}
</style>
{% endblock %}
{% block content %}
<section class="proposition">
<form action="{% url 'article' Article.id %}" method="post" class="propform">
{% csrf_token %}
{{ propform.as_p }}
<button type="submit" class="butprop" name="propform">Poster votre proposition </button>
</form>
</section>
{% endblock %}
我已经尝试过添加{{ propform.media }}
它也不起作用,根据文档,如果你在 head 中添加脚本也没用。
我的页面加载正常,一切正常,终端没有错误,但我仍然有我的简单 TextArea,而不是我的 CKEditorWidget。我做错了什么和/或忘记了什么?
额外问题:我不需要在我的 CKeditorwidget 中上传任何内容。我可以删除“ckeditor_uploader”应用程序的哪些设置和 URL?MEDIA_URL、MEDIA_ROOT、CKEDITOR_UPLOAD_PATH、CKEDITOR_IMAGE_BACKEND?'ckeditor_uploader.urls'?
感谢您的时间和您的回答