- 显示所有评论
{% for foo in ques.comments %}
html文件:
<h4>评论:({{ ques.comments|length }})</h4> <ul class="list-unstyled"> {% for foo in ques.comments %} <li class="list-group-item"> <a href="{{ url_for(‘selfinfo‘,user_id = foo.author.id) }}">{{ foo.author.username }}</a> <span class="badge pull-right">{{ foo.create_time }}</span> <p>{{ foo.detail }}</p> <br> </li> {% endfor %} </ul>
- 所有评论排序
uquestion = db.relationship(‘Question‘, backref=db.backref(‘comments‘, order_by=creat_time.desc))
- 所有评论排序
py文件:
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‘)) create_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=create_time.desc)) author = db.relationship(‘User‘,backref=db.backref(‘comments‘))
- 显示评论条数
{{ ques.comments|length }}
<div class="col-md-8 column" id="rgba1"> <h3 align="center">问答区域</h3> <ul> {% for foo in question %} <li> <span><img src="../static/img/touxiang.jpg " width="30px" alt=""></span><a href="{{ url_for(‘selfinfo‘,user_id = foo.author_id) }}">{{ foo.author.username }}</a> <br> <p>标题:<a href="{{ url_for(‘detail‘,question_id=foo.id) }}">{{ foo.title }}</a></p> <br> <p>内容:{{ foo.detail }}</p> <span>评论数: ({{ foo.comments|length }})</span> <span class="badge pull-right">{{ foo.create_time }}</span> <hr> </li> {% endfor %} </ul> </div>
- 完成个人中心
1.个人中心的页面布局(html文件及相应的样式文件)
2.定义视图函数def usercenter(user_id):
3.向前端页面传递参数
4.页面显示相应数据
发布的全部问答
发布的全部评论
个人信息
5.各个页面链接到个人中心
py文件:
@app.route(‘/selfinfo/<user_id>‘) @loginFirst def selfinfo(user_id): user = User.query.filter(User.id==user_id).first() context = { ‘username‘:user.username, ‘questions‘:user.question,#用反向定义的question ‘comments‘:user.comments } return render_template(‘selfinfo.html‘,**context)
html文件:
{% extends‘myweb.html‘ %} {% block selfinfotitle %}个人页面{% endblock %} {% block selfinfohead %} <link rel="stylesheet" type="text/css" href="../static/css/component.css"/> <script src="../static/js/regist.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> {% endblock %} {% block selfinfobody %} <div class="container"> <div class="row clearfix"> <div class="col-md-2 column"> </div> <div class="col-md-8 column" id="rgba1"> <h2 align="center">个人中心</h2> <p class="text-center"><small>{{ username }}</small></p> <hr> <h3 align="center"> <small>全部问答</small> </h3> <ul class="list-unstyled"> {% for foo in questions %} <li class="list-group-item"> <span class="glyphicon glyphicon-user"></span><a>{{ foo.author.username }}</a> <p>标题:{{ foo.title }}</p> <span class="badge pull-right">{{ foo.create_time }}</span> <p>问答内容:{{ foo.detail }}</p> <br> </li> {% endfor %} </ul> <h3 align="center"> <small>全部评论</small> </h3> <ul class="list-unstyled"> {% for foo in comments %} <li class="list-group-item"> <span class="badge pull-right">{{ foo.create_time }}</span> <p>文章标题:{{ foo.question.title }}</p> <p>评论内容:{{ foo.detail }}</p> <span class="glyphicon glyphicon-user"></span><small ><a>{{ foo.author.username }}</a></small> <br> </li> {% endfor %} </ul> <h3 align="center"> <small>个人信息</small> </h3> <ul class="list-group"> <li class="list-group-item"><span class="glyphicon glyphicon-user"></span>用户:{{ username }}</li> <li class="list-group-item">昵称</li> <li class="list-group-item">文章篇数:{{ questions|length }}</li> </ul> </div> <div class="col-md-2 column"> </div> </div> </div> {% endblock %}
运行截图: