评论列表显示及排序,个人中心显示
- 显示所有评论
{% 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'base.html' %} {% block title %} 首页 {% endblock %} {% block head %} <script src="{{url_for('static',filename='js/base.js') }}" type="text/javascript"></script> <link href="{{ url_for('static' ,filename='css/base.css') }}" rel="stylesheet" type="text/css"> {% endblock %} {% block main %} {% for foo in question %} <body id="myBody"> <div id="gufeng"> <a href="{{ url_for('usercenter',user_id = foo.author_id) }}">{{ foo.author.username }}评论({{ foo.comments|length }})</a> <ul class="list" > <li>用户名:{{ foo.author.username }}</li> <a href="{{ url_for("detail",question_id=foo.id) }}">标题:{{ foo.title }}</a> <li class="">问题:{{ foo.detail }}</li> <li class="">时间:{{ foo.time }}</li> </ul> </div> {% endfor %} {% endblock %}
% extends "base.html" %} {% block title%}详细内容{% endblock %} {% block main %} <body bgcolor="#ffd700"> <div class="GuFeng"> <a href="{{ url_for('usercenter',user_id = foo.author.id) }}">{{ foo.author.username }}</a> { <form action="{{ url_for('/detail/<question_id>') }}" method="post">} <h2>标题:{{ ques.title }}</h2><br> <h3>用户名:{{ ques.author.username }}</h3><br> <h3> 发布时间:{{ ques.create_time }}</h3> <p>内容:{{ ques.detail }}</p> <hr> <textarea class='comment'rows="10"id="detail"name="detail"></textarea> <br><input type="submit" value="发布"style="width:100px;height:50px;font-size:50px"> <input name="question_id" type="hidden" value="{{ ques.id }}"/> <p>评论:</p> <table border=5 style="background: gold"width="50"> <tr><td>评论内容</td></tr> <tr><td>评论内容</td> </tr> <tr><td>评论内容</td></tr> </tr> </table> </div> </body> {% endblock %}
{% extends'base.html' %} {% block title %} 个人中心 {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/user.css')}}" type="text/css"> {% endblock %} {% block main %} <div class="all question"> <h2><a href="{{ url_for('usercenter',user_id=user.id) }}"> {{ user.username }}</a>全部问答</h2> <ul class="wenda" style="width: auto"> {% for foo in user.question %} <li class="wenti"> <a href="#">{{ foo.author.username }} </a> <span>{{ foo.creat_time }}</span><br> <a class="title" href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a><br> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="all detail" > <h2><a href="{{ url_for('usercenter',user_id=user.id) }}"> {{ user.username }}</a>全部评论</h2> <ul class="pinglun" style="width: auto"> {% for foo in user.comments %} <li class="comment"> <a href="#">{{ foo.author.username }} </a> <span>{{ foo.creat_time }}</span><br> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="usercenter"> <h2><a href="{{ url_for('usercenter',user_id=user.id) }}"> {{ user.username }}</a>个人中心</h2> <ul class="yonghu" style="width: auto"> <li><p>用户:{{ user.username }}</p></li> <li><p>编号:{{ user.id }}</p></li> <li><p>昵称:{{ user.nickname}}</p></li> <li><p>文章篇数:{{ user.question|length }}</p></li> </ul> </div> {% endblock %}
@app.route('/usercenter/<user_id>') @loginFirst def usercenter(user_id): user=User.query.filter(User.id==user_id).first() context={ 'user':user } return render_template('center.html',**context)