1.文章添加页url开设
2.添加文章页面已经富文本编辑器的使用
富文本编辑器kindeditor只要到官网下载下来,放入static文件夹就行,如何在html的script处添加对应固定代码
{% extends 'backend/base.html' %}
{% block article %}
<h3>添加随笔</h3>
<form action="" method="post">
<p>标题</p>
<div>
<input type="text" class="form-control" name="title">
</div>
<p>内容</p>
<div>
<textarea name="content" id="editor_id" cols="30" rows="10"></textarea>
</div>
<p>分类</p>
<div>
{% for category in category_list %}
<input type="radio" value="{{ category.pk }}" name="category">{{ category.name }}
{% endfor %}
</div>
<p>标签</p>
<div>
{% for tag in tag_list %}
<input type="checkbox" value="{{ tag.pk }}" name="tag">{{ tag.name }}
{% endfor %}
</div>
<br>
<input type="submit" class="btn-danger btn">
</form>
{% endblock %}
{% block js %}
<script charset="utf-8" src="/static/kindeditor-4.1.11-zh-CN/kindeditor/kindeditor-all-min.js"></script>
<script>
KindEditor.ready(function (K) {
window.editor = K.create('#editor_id',
{
width: '100%',
height:'600px',
resizeType:1,
});
});
</script>
{% endblock %}
3.后端需要传入到前端的数据
4.添加文章向后端发送请求
@login_required
def add_article(request):
category_list = models.Category.objects.filter(blog=request.user.blog)
tag_list = models.Tag.objects.filter(blog=request.user.blog)
if request.method == 'POST':
title = request.POST.get('title')
content = request.POST.get('content')
category_id = request.POST.get('category')
tag_id = request.POST.getlist('tag')
# 文章简介的获取
desc = content[0:150]
article_obj = models.Article.objects.create(title=title,desc=desc,content=content,category_id=category_id,blog=request.user.blog)
# 下一步添加文章和文章标签表,这个关系表是我们自己创建的无法使用add等方法
# 解决方法自己操作关系表,因为是多对多关系,可能需要创建多条数据 采用了批量插入数据的方法
article_obj_list = []
for i in tag_id:
article_obj_list.append(models.Article2Tag(article=article_obj,tag_id=i))
models.Article2Tag.objects.bulk_create(article_obj_list)
return HttpResponseRedirect('/backend/')
return render(request,'backend/add_article.html',locals())
标签:category,文本编辑,list,request,添加,文章,article,tag,id
From: https://www.cnblogs.com/suncolor/p/16777620.html