1.文章添加页url开设

image

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 }}&nbsp;
            {% endfor %}
        </div>
        <p>标签</p>
        <div>
            {% for tag in tag_list %}
                <input type="checkbox" value="{{ tag.pk }}" name="tag">{{ tag.name }}&nbsp;
            {% 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.后端需要传入到前端的数据

image

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()) 

原文地址:https://www.cnblogs.com/suncolor/p/16777620.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性