评论列表显示及排序,个人中心显示
- 显示所有评论
{% for foo in ques.comments %} - 所有评论排序
uquestion = db.relationship('Question', backref=db.backref('comments', order_by=creat_time.desc)) - 显示评论条数
{{ ques.comments|length }} - 完成个人中心
1.个人中心的页面布局(html文件及相应的样式文件)
2.定义视图函数def usercenter(user_id):
3.向前端页面传递参数
4.页面显示相应数据
发布的全部问答
发布的全部评论
个人信息
5.各个页面链接到个人中心
{% extends 'basic.html' %} {% block title %}个人中心{% endblock %} {% block usercenter %} <div class="card touming" style="width:50rem; margin:0 auto"> <ul class="list-group list-group-flush"> <h3 style="margin:0" align="center">ANSWER</h3> {% for foo in user.questions %} <li class="list-group-item"> <span class="icon" aria-hidden="true"></span> <br> <h3> <a href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a></h3> <p>{{ foo.detail }}</p> <span class="badge"style="float:right">{{ foo.creat_time}}</span> <a href="{{ url_for('usercenter',user_id=foo.author.id) }}">{{ foo.author.username}}</a> </li> {% endfor %} </ul> </div> <br> <br> <div class="card touming" style="width:50rem; margin:0 auto"> <ul class="list-group list-group-flush"> <h3 style="margin:0" align="center">COMMENT</h3> {% for foo in user.comments %} <li class="list-group-item"> <span class="icon" aria-hidden="true"></span> <br> <h3> <a href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a></h3> <p>{{ foo.detail }}</p> <span class="badge"style="float:right">{{ foo.creat_time}}</span> <a href="">{{ foo.author.username}}</a> </li> {% endfor %} </ul> </div> <br> <br> <div class="card touming" style="width:50rem; margin:0 auto"> <h3 style="margin:0" align="center" color="blue">{{ username }}MESSAGE</h3> <ul class="list-group list-group-flush"> <li class="list-group-item"> <span class="icon" aria-hidden="true"> 用户:{{ user.username }}<br> 编号:{{ user.id }}<br> 昵称:{{ user.nickname }}<br> 文章篇数:{{ user.questions|length }} </span> </li> </ul> </div> {% endblock %}
def usercenter(user_id): user=User.query.filter(User.id==user_id).first() context={ # 'username':user.username, # 'question':user.questions, # 'comments':user.comment 'user':user } return render_template('usercenter.html',**context)