1.显示所有评论
{% for foo in ques.comments %}
<h3>评论区: ({{ ques.comments|length }}) </h3><br> <div class="basic_box" style="padding-bottom: 50px;"> <ul class="list-group" style="margin-bottom: 10px"> {% for qu in ques.comments %} <li class="list-group-item" style="width: 800px"> <a class="wrap-img" href="#" target="_blank"> <img src="{{ qu.author.image }}" width="50px"> </a> <span class="glyphicon glyphicon-left" aria-hidden="true"></span> <br> <a href="{{ url_for(‘person‘,user_id=qu.author.id) }}">{{ qu.author.username }}</a> <span class="badge">评论时间:{{ qu.creat_time }}</span> <p style="">{{ qu.detail }} </p> </li> {% endfor %} </ul>
2.所有评论排序
uquestion = db.relationship(‘Question‘, backref=db.backref(‘comments‘, order_by=creat_time.desc))
class Comment(db.Model): __tablename__ = ‘comment‘ id = db.Column(db.Integer, primary_key=True, autoincrement=True) author_id = db.Column(db.Integer, db.ForeignKey(‘user.id‘)) sent_id = db.Column(db.Integer, db.ForeignKey(‘sent.id‘)) creat_time = db.Column(db.DateTime, default=datetime.now) detail = db.Column(db.TEXT, nullable=False) sent = db.relationship(‘Sent‘, backref=db.backref(‘comments‘,order_by=creat_time.desc)) author = db.relationship(‘User‘, backref=db.backref(‘comments‘))
3.显示评论条数
{{ ques.comments|length }}
<div class="container"> <div class="row clearfix"> <div class="col-md-2 column"> </div> <div class="col-md-6 column"> <h1><img src="{{ img }}" width="50px">{{usern }}</h1> <br>全部问答 <div class="basic_box" style="padding-bottom: 50px;"> <ul class="list-group"> {% for qu in ques %} <li class="list-group-item" style="width: 800px"> <a class="wrap-img" href="#" target="_blank"> <img src="{{ qu.author.image }}" width="50px"> </a> <span class="glyphicon glyphicon-left" aria-hidden="true"></span> <a href="{{ url_for(‘person‘,user_id=qu.author.id) }}" target="_blank">{{ qu.author.username }}</a> <br> <a href="{{ url_for(‘detail‘,question_id=qu.id) }}">{{qu.title }}</a> <br> <span class="badge">发布时间:{{ qu.creat_time }}</span> <p style="">{{ qu.detail }} </p> </li> {% endfor %} </ul> </div> <h1><img src="{{ img }}" width="50px">{{usern }}</h1> <br>全部评论 <div class="basic_box" style="padding-bottom: 50px;"> <ul class="list-group" style="margin-bottom: 10px"> {% for qu in users %} <li class="list-group-item" style="width: 800px"> <a class="wrap-img" href="#" target="_blank"> <img src="{{ qu.author.image }}" width="50px"> </a> <span class="glyphicon glyphicon-left" aria-hidden="true"></span> <br> <a href="#">{{ qu.author.username }}</a> <span class="badge">评论时间:{{ qu.creat_time }}</span> <p style="">{{ qu.detail }} </p> </li> {% endfor %} </ul> <br> <br> <h1><img src="{{ img }}" width="50px">{{usern }}</h1> <br>个人信息 <div class="basic_box" style="padding-bottom: 80px;"> <ul class="list-group" style="margin-bottom: 10px"><li class="list-group-item" style="width: 800px"> 用户:{{ usern }}</li> <li class="list-group-item" style="width: 800px"> 发布问答:{{ ques|length }}</li> <li class="list-group-item" style="width: 800px"> 评论:{{ comment|length }}</li></ul> </div> <div class="col-md-4 column"> </div> </div> </div> </div> </div>
4.完成个人中心
用户:
<div class="all have-img"> <div> {% for foo in username %} <h4>用户名:{{ foo.author.username }} <br> <small>全部问答</small> </h4> <ul style="padding-left: 0px;margin-bottom: 0px;"> <li class="list-group-item" style="width: 900px"> <a href="">{{ foo.author.username }}</a> <span class="badge">评论时间:{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> </ul> </div> <hr> <div> {% for foo in comment %} <h4>用户名:{{ foo.author.username }} <br> <small>全部评论</small> </h4> <ul style="padding-left: 0px;margin-bottom: 0px;"> <li class="list-group-item" style="width: 900px"> <a href="#">{{ foo.author.username }}</a> <span class="badge">评论时间:{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> </ul> </div> <hr> <div> <h4>{{ user }} <br> <small>用户资料</small> </h4> <ul style="padding-left: 0px;margin-bottom: 0px;"> <li class="list-group-item" style="width: 900px">用户:{{ username }}</li> <li class="list-group-item" style="width: 900px">编号:</li> <li class="list-group-item" style="width: 900px">昵称:</li> </ul> </div> </div>
主函数
@app.route(‘/usercenter/<user_id>‘) @loginFirst def usercenter(user_id): user=User.query.filter(User.id==user_id).first() context={ ‘username‘:user.username, ‘sent‘:user.sent, ‘comments‘:user.comments } return render_template(‘geren.html‘,**context)
向前端页面传递参数
<input type="hidden" name="sent_id" id="sent_id" value="{{ sen.id }}">
各个页面链接到个人中心
#首页用户名链接到个人中心 <a class="nickname" target="_blank" href="{{ url_for(‘usercenter‘,user_id=foo.author_id) }}">{{ foo.author.username }}</a> #详情用户名连接到个人中心 <a href="{{ url_for(‘usercenter‘,user_id=foo.author.id) }}">{{ foo.author.username }}</a>