- 显示所有评论
{% for foo in ques.comments %}
<h4>评论:({{ quest.comments|length }})</h4> <ul class="news-list"> {% for foo in quest.comments %} <li class="list-group-item"> <a href="#">{{ foo.author.username }} </a> <span class="badge">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul>
- 所有评论排序
uquestion = db.relationship('Question', backref=db.backref('comments', order_by=creat_time.desc)) - 显示评论条数
{{ ques.comments|length }}
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')) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) creat_time = db.Column(db.DateTime, default=datetime.now) detail = db.Column(db.Text, nullable=False) question = db.relationship('Question', backref=db.backref('comments', order_by=creat_time.desc)) author = db.relationship('User', backref=db.backref('comments'))
- 完成个人中心
1.个人中心的页面布局(html文件及相应的样式文件)
{% extends 'base.html' %} } {% block title %}用户中心{% endblock %} {% block head %} <!-- 引入 Bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> {% endblock %} {% block main %} <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span> {{ username }} <br> <small>all questions</small> </h3> <ul class="news-list"> {% for foo in questions %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart" aria-hidden="true"></span> <a href="#">{{ foo.author.username }} </a> <span class="badge">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span> {{ username }} <br> <small>all comments</small> </h3> <ul class="news-list"> {% for foo in comments %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart" aria-hidden="true"></span> <a href="#">{{ foo.author.username }} </a> <span class="badge">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span> {{ user }} <br> <small>User</small> </h3> <ul class="news-list"> <li class="list-group-item"> <span class="glyphicon glyphicon-heart" aria-hidden="true"></span> Username: {{ username }} </li> <li class="list-group-item"> <span class="glyphicon glyphicon-heart" aria-hidden="true"></span> Nickname: {{ nickname }} </li> </ul> </div> {% endblock %}
2.定义视图函数def usercenter(user_id):
@app.route('/usercenter/<user_id>',methods=['GET']) @loginFrist def usercenter(user_id): user=User.query.filter(User.id==user_id).first() context={ 'username':user.username, 'nickname':user.nickname, 'questions':user.question, 'comments':user.comments } return render_template('usercenter.html',**context)
3.向前端页面传递参数
4.页面显示相应数据
发布的全部问答
发布的全部评论
个人信息
5.各个页面链接到个人中心
<a href="{{ url_for('usercenter',user_id=foo.author.id) }}">{{ foo.author.username }} 评论:({{ foo.comments|length }})</a>