- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
@app.route('/shouye/')
def shouye():
context = {
'questions':Question.query.order_by('create_time').all()
}
return render_template('shouye.html',**context)
- 前端页面循环显示整个列表。
<ul class="list-group">
{% for foo in questions %}
<li class="list-group-item">
<img style="width: 30px" src="{{ url_for('static',filename='css/touxiang.jpg') }}" alt="64">
<a href="#">{{ username }}</a><br>
<a href="#">问题:{{ foo.title }}</a><br>
<p style="align-content: center">{{ foo.detail }}</p>
<span class="badge" style="margin-left: 60%">{{ foo.create_time }}</span>
<p style="margin-left: 25%"></p><br>
</li>
{% endfor %}
</ul>
- 问答排序
'questions':Question.query.order_by('create_time').all()
- 完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
- 在首页点击问答标题,链接到相应详情页
{% extends 'ba.html' %}
{% block title %}
问答详情
{% endblock %}
{% block main %}
<div class="box">
<h3>Title{{ ques }}
<br><small>author r</small></h3><hr>
<p>detail</p>
<hr>
<form action="{{ url_for('detail') }}" method="post">
<div><textarea class="form-control" id="new_comment" rows="3" style="margin-left: 1%" name="new_comment" placeholder="write your comment"></textarea><br></div>
<button type="submit" onclick="foLogin()">发送</button>
</form>
<ul class="list-group" style="margin: 10px"></ul>
</div>
{% endblock %}
![]()
![]()