一、顯示全部評論
{% for foo in ques.comments %}html
{% for foo in comment %} <ul style="padding-left: 0px;margin-bottom: 0px;"> <li style="width: 900px"> <a href="{{ url_for('usercenter',user_id = foo.author.id) }}">{{ foo.author.username }}</a> <span class="badge">評論時間:{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> </ul> {% endfor %}
二、全部評論排序
uquestion = db.relationship('Question', backref=db.backref('comments', order_by=creat_time.desc))前端
question = db.relationship('Question',backref = db.backref('comments',order_by=creat_time.desc))
三、顯示評論條數
{{ ques.comments|length }}python
<h4>評論:({{ quest.comments|length }})</h4>
四、完成我的中心app
1.我的中心的頁面佈局(html文件及相應的樣式文件)函數
2.定義視圖函數def usercenter(user_id):佈局
3.向前端頁面傳遞參數url
4.頁面顯示相應數據spa
發佈的所有問答code
發佈的所有評論htm
我的信息
<!DOCTYPE html> <html lang="en"> <head> {% extends 'admin.html' %} <meta charset="UTF-8"> <title> {% block title %} 用戶中心 {% endblock %} </title> {% block head %} {% endblock %} </head> <body> {% block body %} <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h3>{{ username }}><br> <small>所有問答 <span class="badge"></span></small> </h3> <ul> {% for foo in question %} <li> <a href="#">{{ foo.username }}</a> <span class="badge">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> </div> </div> <div> <h3>{{ username }}><br> <small>所有評論 <span class="badge"></span></small> </h3> {% for foo in comments %} <ul style="padding-left: 0px;margin-bottom: 0px;"> <li style="width: 800px"> <a href="">{{ foo.author.username }}</a> <span class="badge">{{ foo.creat_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div> <h3>{{ user }}<br> <small>我的信息</small> </h3> <ul style="padding-left: 0px;margin-bottom: 0px;"> <li>用戶:</li> <li>編號:</li> <li>暱稱:</li> </ul> </div> </div> {% endblock %} </body> </html>
@app.route('/usercenter/<user_id>') @loginFirst def usercenter(user_id): user = User.query.filter(User.id == user_id).first() context = { 'username':user.username, 'question':user.question, 'comments':user.comments } return render_template('usercenter.html',**context)
五、各個頁面連接到我的中心
<div class="meta"> <a class="collection-tag" target="_blank" href="/c/1hjajt">{{ biaoqian }}</a> <a href="{{ url_for('usercenter',user_id = foo.author_id) }}">{{ foo.author.username }}評論:({{ foo.comments|length}})</a> </div>