- 显示所有评论
{% 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.各个页面链接到个人中心
py文件:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
... # 跳转用户详情 @app.route('/yonghu/<username_id>') def yonghu(username_id): user = User.query.filter(User.id == username_id).first() context = { 'username':user.username, 'fabus':user.fabu, 'comments':user.comments } return render_template('yonghu.html',**context) ...
daohang.html:(主页)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
... <ul class="list-unstyled"> {% for foo in fabus %} <li class="list-group-item-success"> <a href="{{ url_for('yonghu',username_id=foo.author_id) }}"><span class="glyphicon glyphicon-fire"></span>{{ foo.author.username }}</a> <h4 class="text-center"><a href="{{ url_for('fabuview',fabu_id=foo.id) }}">{{ foo.title }}</a></h4> <span class="badge pull-right">{{ foo.creat_time }}</span> <br> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> ...
fabuview.html:(发布详情页)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
{% extends 'daohang.html' %} {% block fabu_viewtitle %}发布内容{% endblock %} {% block fabu_viewhead %} <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> {% endblock %} {% block daohangbody %} <div class="col-md-2 column "></div> <div class="col-md-8 column "> <h2 href="#" class="text-center">{{ fa.title }}</h2> <br> <p class="text-center"> <a href="{{ url_for('yonghu',username_id=fa.author_id) }}"> <small>{{ fa.author.username }}</small> </a>    <span class="pull-center"><small>{{ fa.creat_time }}</small></span> </p> <hr> <p>{{ fa.detail }}</p> <hr> <form action="{{ url_for('comment') }}" method="post"> <div class="form-group"> <textarea name="pinglun" class="form-control" rows="5" id="pinglun" placeholder="请输入评论"></textarea> <input type="hidden" name="fa_id" value="{{ fa.id }}"> </div> <button type="submit" class="btn btn-default">发送</button> <br> <br> <h4>评论:({{ fa.comments|length }})</h4> <ul class="list-unstyled"> {% for foo in fa.comments %} <li class="list-group-item"> <a href="{{ url_for('yonghu',username_id=foo.author_id) }}"><span class="glyphicon glyphicon-fire"></span>{{ foo.author.username }}</a> <span class="badge pull-right">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> <br> </li> {% endfor %} </ul> </form> <br> <br> <br> </div> <div class="col-md-2 column "></div> {% endblock %}
yonghu.html:(用户详情页)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
{% extends 'daohang.html' %} {% block yonghutitle %}用户详情{% endblock %} {% block yonghuhead %} <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> {% endblock %} {% block daohangbody %} <div class="col-md-2 column "></div> <div class="col-md-8 column "> <h2 href="#" class="text-center">个人信息</h2> <hr> <p><span class="glyphicon glyphicon-fire"></span>username</p> <br> <div class="col-md-6 column s1"> <h3 class="text-center">全部发布信息({{ fabus|length }})</h3> <ul class="list-unstyled"> {% for foo in fabus %} <li class="list-group-item"> <a href="{{ url_for('yonghu',username_id=foo.author_id) }}"><span class="glyphicon glyphicon-fire"></span>{{ foo.author.username }}</a> <h4 class="text-center"><a href="{{ url_for('fabuview',fabu_id=foo.id) }}">{{ foo.title }}</a> </h4> <span class="badge pull-right">{{ foo.creat_time }}</span> <br> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> <br> <br> <br> </div> <div class="col-md-6 column s1"> <h3 class="text-center">全部评论信息({{ comments|length }})</h3> <ul class="list-unstyled"> {% for foo in comments %} <li class="list-group-item"> <a href="{{ url_for('yonghu',username_id=foo.author_id) }}"><span class="glyphicon glyphicon-fire"></span>{{ foo.author.username }}</a> <span class="badge pull-right">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> <br> </li> {% endfor %} </ul> <br> <br> <br> </div> </div> <div class="col-md-2 column "></div> {% endblock %}