一. 用户中心- 关于我设置
1. templates / user / center.html 填充用户中心关于我的设置html
<!-- 关于我的设置-->
<div class="right1" style="padding-top:30px;">
<form action="{{ url_for('user.about_me') }}" method="post" enctype="multipart/form-data">
<p>
<textarea name="about" class="mytextarea">
</textarea>
</p>
<p></p>
<p style="text-align:right;">
<button type="submit" class="btn btn-primary">更改我的</button>
</p>
</form>
</div>
2. apps / user /view.py 编写关于我的函数
# 关于用户介绍添加
@user_bp.route('/aboutme', methods=["GET", "POST"])
def about_me():
content = request.form.get("about")
# 添加信息
try:
aboutme = AboutMe()
# 模型类型为BLOB二进制,需要转成二进制才能成功储存
# 或者模型类型改为TEXT,数据库类型为BLOB
aboutme.content = content.encode("utf-8")
aboutme.user_id = g.user.id
db.session.add(aboutme)
db.session.commit()
except Exception as err:
return redirect(url_for('user.user_center'))
else:
return render_template('user/aboutme.html', user=g.user)
二 . 首页 - 关于我页面
1. templates / user / aboutme.html 新建关于我页面
(1). 继承公共模板
{% extends 'base.html' %}
{% block title %}
关于我
{% endblock %}
(2). 编写样式
{% block styles %}
{{ super() }}
<style>
#container {
width: 1000px;
margin: 0 auto;
}
#container p {
width: 80%;
background-color: #83cae3;
}
</style>
{% endblock %}
(3). 编写页面
{% block newcontent %}
<div id="container">
<p>
{% if not user.about %}
用户中心设置更多关于我的信息~~~
{% else %}
{{ user.about[0].content | cdecode1 | safe }}
{% endif %}
</p>
</div>
{% endblock %}
2. apps / user / view.py 编写关于我函数
# 关于我页面展示
@user_bp.route("/showabout")
def show_about():
return render_template("user/aboutme.html", user=g.user)
3. base.html 配置关于我的路由
<li><a href="{{ url_for('user.show_about') }}">关于我</a></li>