Python 3.x django ckeditor赢得';表格上没有显示
遵循中提到的每个步骤,但是当我查看表单时,没有任何编辑器。我还运行了“python manage.py collectstatic” settings.py特定于ckeditorPython 3.x django ckeditor赢得';表格上没有显示,python-3.x,django-forms,ckeditor5,Python 3.x,Django Forms,Ckeditor5,遵循中提到的每个步骤,但是当我查看表单时,没有任何编辑器。我还运行了“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 = STA
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')
url.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开发者工具-->元素时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>
您将以下两行放错了位置
brief = forms.CharField(widget=CKEditorWidget())
content = forms.CharField(widget=CKEditorWidget())
它们必须在元类之外,因此如下所示
class BlogPostForm(forms.ModelForm):
brief = forms.CharField(widget=CKEditorWidget())
content = forms.CharField(widget=CKEditorWidget())
class Meta():
model = BlogPost
fields = ('title','brief','content','accept_comments','is_public')
您在下面两行中放错了位置
brief = forms.CharField(widget=CKEditorWidget())
content = forms.CharField(widget=CKEditorWidget())
它们必须在元类之外,因此如下所示
class BlogPostForm(forms.ModelForm):
brief = forms.CharField(widget=CKEditorWidget())
content = forms.CharField(widget=CKEditorWidget())
class Meta():
model = BlogPost
fields = ('title','brief','content','accept_comments','is_public')
Ck编辑器
第1步:pip安装django-ckeditor
第二步: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和migrate
步骤5:现在检查管理页面,ckeditor
在textfield中应用
如果要在html文件中添加ckeditor
步骤6:在forms.py中
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title','text',)
步骤7:在html文件中添加两行代码:
{{ form.text | safe }}
{{ form.media }}
Ck编辑器
第1步:pip安装django-ckeditor
第二步: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和migrate
步骤5:现在检查管理页面,ckeditor
在textfield中应用
如果要在html文件中添加ckeditor
步骤6:在forms.py中
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title','text',)
步骤7:在html文件中添加两行代码:
{{ form.text | safe }}
{{ form.media }}
我建议使用本教程。
在我的例子中,我创建了一个模型:
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})
然后在my 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>
{%csrf_令牌%}
{{form.media}
{{form}}
邮递
我建议学习本教程。
在我的例子中,我创建了一个模型:
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})
然后在my 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>
{%csrf_令牌%}
{{form.media}
{{form}}
邮递
没什么区别。没什么区别。