Flask CKEditor 富文本编译器实现文章的图片上传以及回显,解决路径出错的问题

Flask CKEditor 富文本编译器实现文章的图片上传以及回显,解决路径出错的问题

下面是效果图:
在这里插入图片描述

在这里插入图片描述
上代码,这个是视图部分

@admin.route("/admin/match", methods=['GET', 'POST'])
def match():
    new_h_load = ""
    form = MatchForm()
    if request.method == "POST":
        if form.submit.data:
            title = request.form.get('title')
            name = request.form.get("name")
            message = request.form.get('body')
        elif form.sent.data:
            title = request.form.get('title')
            name = request.form.get("name")
            message = request.form.get('body')
            QueryMatch().add(title, message, name)
            return render_template("admin/skip.html")
        elif form.upload.data:
            f = request.files.get('file')
            filename = secure_filename(f.filename)
            ext = filename.split('.', 1)[1]
            unix_time = int(time.time())
            new_filename = str(unix_time) + '.' + ext
            load = url_for('static', filename=r'img/essay/match')
            new_load = "./." + load
            h_load = path.join(new_load, new_filename).replace('\\', '/')  # 保存的地址
            new_h_load = h_load.replace("./", "../")
            print(h_load)
            f.save(h_load)
            return render_template("admin/match.html", form=form, new_h_load=new_h_load)
        return render_template("admin/match.html", form=form, new_h_load=new_h_load)
    else:
        return render_template("admin/match.html", form=form, new_h_load=new_h_load)

模板部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章发布中心</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
	<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
	<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function() {
        $("#content2").html($("#content1").text());
        });
    </script>
    <style type="text/css">
        .head{
            width: 100%;
            height: 50px;
            text-align: center;
            border-radius: 20px;
            -moz-box-shadow:0 0 10px #010a13;
            -webkit-box-shadow:0 0 10px #01060a;
            box-shadow:0 0 10px #00050a;
            margin-bottom: 20px;
        }
        .title{
            margin: auto;
            width: 100%;
            height: 60px;
            text-align: center;
            border-radius: 20px;
            -moz-box-shadow:0 0 10px #010a13;
            -webkit-box-shadow:0 0 10px #01060a;
            box-shadow:0 0 10px #00050a;
        }
        .title1{
            margin: auto;
            width: 100%;
            height: 60px;
            border-radius: 20px;
            -moz-box-shadow:0 0 10px #010a13;
            -webkit-box-shadow:0 0 10px #01060a;
            box-shadow:0 0 10px #00050a;
        }
        .Body{
            margin-top: 20px;
            width: 100%;
            height: 60%;
            border-radius: 20px;
            -moz-box-shadow:0 0 10px #010a13;
            -webkit-box-shadow:0 0 10px #01060a;
            box-shadow:0 0 10px #00050a;
        }
        .input{
            height: 30px;
            border-radius: 10px;
            -moz-box-shadow:0 0 10px #3291ef;
            -webkit-box-shadow:0 0 10px #4ba5ee;
            box-shadow:0 0 10px #4294e7;
        }
        .input2{
            width: 60px;
        }

        .foot{
            float: right;
        }
        .div1{
            float: right;
        }
        .div2{
            display: flex;
        }
        .div3{
            width: 50%;
            border-radius: 4px;
            -moz-box-shadow:0 0 10px #010a13;
            -webkit-box-shadow:0 0 10px #01060a;
            box-shadow:0 0 10px #00050a;
        }
        .div4{
            width: 48%;
            margin-left: 20px;
            border-radius: 4px;
            -moz-box-shadow:0 0 10px #010a13;
            -webkit-box-shadow:0 0 10px #01060a;
            box-shadow:0 0 10px #00050a;
        }
        .div5{
            margin-top: 20px;
            -moz-box-shadow:0 0 10px #577b97;
            -webkit-box-shadow:0 0 10px #577b97;
            box-shadow:0 0 10px #6a91b8;
        }
    </style>
</head>
<body>
<div class="container-fluid div2">
    <div class="div3">
        <div class="head">
            <h1>文章发布中心</h1>
        </div>
        <div>
            <form method="post" enctype="multipart/form-data">
                {{ form.csrf_token }}
                <div class="title">
                    {{ form.title(class="input") }}<br>
                    <div class="div1">
                        编辑人:{{ form.name(class="input input2") }}
                    </div>
                </div>
                <div class="title">
                    {{ form.file }}{{ form.upload }}
                    <span style="float: right">图片地址:{{ new_h_load }} </span>&nbsp;&nbsp;
                </div>
                <div class="Body">
                    {{ form.body }}
                </div>
                <div class="foot">
                    {{ form.submit(class='btn btn-success') }}
                    {{ form.sent(class='btn btn-success') }}
                </div>
            </form>
        </div>
    </div>

    <div class="div4">
        <div class="head">
            <h1>文章预览效果</h1>
        </div>
        <div class="title">
            <h1 style="text-align: center">{{ request.form.get("title") }} </h1>
        </div>
        <div id = "content1"  hidden="hidden">
            {{ request.form.get('body') }}
        </div>
        <div id = "content2" class="div5">
        </div>


    </div>
</div>
{% block scripts %}
{{ ckeditor.load() }}
{% endblock %}
</body>
</html>

最终效果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值