评论列表显示及排序,个人中心显示
- 显示所有评论
{% 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.各个页面链接到个人中心
@app.route('/user_detail<user_id>') @login_first def user_detail(user_id): user=User.query.filter(User.id == user_id).first() context={ "user":user } return render_template("user_detail.html",**context)
question = db.relationship("Question",backref=db.backref("comment",order_by=create_time.desc))
{% extends "base.html" %} {% block title%}用户详情{% endblock %} {% block head %} <link href="../static/css/user_detail.css" rel="stylesheet" type="text/css"> {% endblock %} {% block main %} <body> <form class="basic"> {% for foo in user.question %} <h2>{{ foo.username }}<span>全部问答</span></h2> <fieldset> <span >{{ foo.create_time }}</span> <h3>{{ foo.title }}</h3> <h3>{{ foo.detail}}</h3> </fieldset> {% endfor %} </form> <form class="basic"> {% for foo in user.comment %} <h2>{{ foo.username }}<span>全部评论</span></h2> <fieldset> <span>{{ foo.create_time }}</span> <h3>{{ foo.detail}}</h3> </fieldset> {% endfor %} </form> <form class="basic"> <fieldset> <span>个人信息</span> <ul> <li>用户:{{ user.username }}</li> <li>编号:{{ user.id }}</li> <li>文章篇数:{{ user.question|length }}</li> </ul> </fieldset> </form> </body> {% endblock %}
<div> <fieldset> <h4 >评论:({{ ques.comment|length }})</h4> <ul class="note"> {% for foo in ques.comment %} <li class="lis"> <div class="author"> <a class="nickname" target="_blank" href="{{ url_for('user_detail',user_id=foo.author_id) }}">{{ foo.author.username }}<span class="time">{{ foo.create_time }}</span></a> <p>{{ foo.detail }}</p> </div> </li> {% endfor %} </ul> </fieldset> </div>