我是 Django 和 CKEditor 的新手,过去 1 周以来一直在努力将 Django-CKEditor 集成到表单中。它在 Django Admin 表单中工作得很好,但在普通表单中不起作用。
这是我的forms.py
class ArticleForm(forms.ModelForm):
content = forms.CharField(widget = CKEditorWidget())
class Meta:
model = Article
fields = ['title','content','meta_description','meta_tags']
模型.py
class Article(models.Model):
title = models.CharField(max_length=500)
url = models.CharField(max_length=500)
date = models.DateTimeField(auto_now_add=True, blank=True)
author = models.CharField(max_length=100)
content = models.TextField()
meta_description = models.TextField()
meta_tags = models.TextField()
is_published = models.BooleanField(default=False)
意见
def new_post(request):
if request.method == 'POST':
form = ArticleForm(request.POST)
if form.is_valid():
article = form.save(commit=False)
article.url = form.data['title']
article.save()
return HttpResponse('/thanks/')
else:
form = ArticleForm()
return render(request, 'blog/new-post.html', {'form': form})
为了验证它是否在管理员中工作,我通过在管理员表单中添加它来测试管理员。
管理员.py
class ArticleAdminForm(forms.ModelForm):
content = forms.CharField(widget=CKEditorWidget())
class Meta:
model = Article
class ArticleAdmin(admin.ModelAdmin):
form = ArticleAdminForm
admin.site.register(Article, ArticleAdmin)
我正在使用 bootstrap3 的另一件事。我的模板看起来像
<form class="form-horizontal" action="/blog/new-post/" method="post" >{% csrf_token %}
<fieldset>
<legend>New Blog Post</legend>
{{ form.non_field_errors }}
<div class="fieldWrapper form-group">
{{ form.title.errors }}
<label class="col-lg-2 control-label" for="id_title">Title</label>
<div class="controls col-lg-10 ">
<input type="text" class="col-lg-10 form-control" name="title" id="id_title}}" placeholder="Title">
</div>
</div>
<div class="fieldWrapper form-group">
{{ form.content.errors }}
<label class="col-lg-2 control-label" for="id_content">Content</label>
<div class="controls col-lg-10 ">
<textarea class="col-lg-10 form-control" rows="17"name="content" id="id_content}}" placeholder="Content"></textarea>
</div>
</div>
<div class="fieldWrapper form-group">
{{ form.meta_description.errors }}
<label class="col-lg-2 control-label" for="id_meta_description">Description</label>
<div class="controls col-lg-10 ">
<textarea class="col-lg-10 form-control" rows="5"name="meta_description" id="id_meta_description}}" placeholder="Short description about this article."></textarea>
</div>
</div>
<div class="fieldWrapper form-group">
{{ form.meta_tags.errors }}
<label class="col-lg-2 control-label" for="id_meta_tags">Tags</label>
<div class="controls col-lg-10 ">
<input type="text" class="col-lg-10 form-control" name="meta_tags" id="id_meta_tags}}" placeholder="Comma separated tags eg. trekking, hiking ">
</div>
</div>
<input type="submit" value="Publish" class="btn btn-default btn-large pull-right">
</fieldset>
</form>
任何帮助都会很棒。提前致谢