遵循https://django-ckeditor.readthedocs.io/en/latest/中提到的每个步骤,但是当我查看表单时,没有任何编辑器。我也确实运行了“ python manage.py collectstatic”
settings.py特定于ckeditor。
INSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,"static")
CKEDITOR_BASEPATH = STATIC_ROOT+"/ckeditor/ckeditor"
CKEDITOR_UPLOAD_PATH = "ck_uploads/"
CKEDITOR_IMAGE_BACKEND = "pillow"
MEDIA_URL = STATIC_URL+'media/'
MEDIA_ROOT = os.path.join(STATIC_ROOT,'media')
urls.py
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
forms.py
from ckeditor.widgets import CKEditorWidget
class BlogPostForm(forms.ModelForm):
class Meta():
model = BlogPost
fields = ('title','brief','content','accept_comments','is_public')
brief = forms.CharField(widget=CKEditorWidget())
content = forms.CharField(widget=CKEditorWidget())
到目前为止,尚无建议(包括将小部件定义移出Meta)。
我是自定义呈现表单的方法,这就是在表单中呈现字段的方式。
{{ form.details }}
使用{{ form.as_p }}
渲染整个表单也没有任何区别。
这是我使用Chrome开发人员工具-> Elements时HTML字段显示的内容。
<textarea cols="40" id="id_details" name="details" rows="10" required="" data-processed="0" data-config="{"skin": "moono-lisa", "toolbar_Basic": [["Source", "-", "Bold", "Italic"]], "toolbar_Full": [["Styles", "Format", "Bold", "Italic", "Underline", "Strike", "SpellChecker", "Undo", "Redo"], ["Link", "Unlink", "Anchor"], ["Image", "Flash", "Table", "HorizontalRule"], ["TextColor", "BGColor"], ["Smiley", "SpecialChar"], ["Source"]], "toolbar": "Custom", "height": 291, "width": 835, "filebrowserWindowWidth": 940, "filebrowserWindowHeight": 725, "toolbar_Custom": [["Bold", "Italic", "Underline"], ["NumberedList", "BulletedList", "-", "Outdent", "Indent", "-", "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock"], ["Link", "Unlink"], ["RemoveFormat", "Source"]], "language": "en-us"}" data-external-plugin-resources="[]" data-id="id_details" data-type="ckeditortype"></textarea>
答案 0 :(得分:2)
步骤1:pip install django-ckeditor
第2步:INSTALLED_APPS = ['ckeditor',]
步骤3:转到==> models.py
并添加文本字段RichTextField
像这样:
from ckeditor.fields import RichTextField
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = RichTextField(blank=True, null=True)
步骤4:python manage.py makemigrations and migrate
第5步:现在检查管理页面,ckeditor
是否适用于文本字段
第6步:在forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title','text',)
步骤7:在html文件中添加两行代码:
{{ form.text | safe }}
{{ form.media }}
答案 1 :(得分:0)
您在以下两行中输入了错误的内容。
Message
它们必须不在Meta类之内,因此如下所示。
brief = forms.CharField(widget=CKEditorWidget())
content = forms.CharField(widget=CKEditorWidget())
答案 2 :(得分:0)
我建议使用本教程by Samuli Natri。 就我而言,我创建了一个模型:
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
class Article(models.Model):
title = RichTextField(blank=True, null=True)
content = RichTextUploadingField(blank=True, null=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def get_absolute_url(self): #redirects to detail view of this post
return reverse("post-detail", kwargs={"pk": self.pk})
,然后在我的views.py中,我有一个基于类的视图,不需要创建表单。
from django.views.generic import(CreateView)
from .models import Article
class ArticleCreateView(CreateView):
model = Article
fields = ['title', 'content']
重要的是,您的html具有以下标记
<form method="POST">
{% csrf_token %}
{{ form.media }} <!-- ckeditor thing -->
{{ form }}
<button class="btn" type="submit">Post</button>
</form>