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>
</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>
最终效果: