1、首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
py文件:
@app.route('/') def moban(): context = { 'wenda': Wenda.query.order_by('creat_time').all() } return render_template('moban.html',**context)
html文件:
<ul class="news-list"> {% for foo in wenda %} <li class="list-group-item"> <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span> <a href="#">{{ foo.author.username}}</a> <br> <a href="{{ url_for('detail',wenda_id= foo.id) }}">{{ foo.title }}</a> <br> <span class="badge" >{{ foo.creat_time }}</span> <p style="background-color: aqua">{{ foo.detail }}</p> </li> {% endfor %} </ul>
2、完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
{% extends 'moban.html' %} {% block title %}详情界面{% endblock %} {% block head %} <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 body %} <div class="col-md-2 column "></div> <div class="col-md-8 column "> <ul class="list-unstyled"> <li> <h2 href="#" class="text-center">{{ wend.title }}</h2> <br> <p class="text-center"> <a href="#"> <small>{{ wend.author.username }}</small> </a>    <span class="pull-center"><small>{{ wend.creat_time }}</small></span> </p> <p>{{ wend.detail }}</p> <form> <div class="form-group"> <textarea name="comment" class="form-control" rows="6" id="comment" placeholder="请输入评论"></textarea> </div> <button type="submit" class="btn btn-default">发送</button> </form> </li> </ul> </div> <div class="col-md-2 column "></div> {% endblock %}
3、在首页点击问答标题,链接到相应详情页。
@app.route('/detail?<wenda_id>') def detail(wenda_id): wen = Wenda.query.filter(Wenda.id == wenda_id).first() return render_template('detail.html',wend=wen)