- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
- 完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
- 在首页点击问答标题,链接到相应详情页。
1.首页列表显示全部问答:
python代码如下:
1 @app.route('/',methods=['GET','POST']) 2 def index(): 3 # context = { 4 # 'userName' : "AllianceHacker", 5 # 'toTime' : '11小时前', 6 # 'title' : 'PHP是世界是最好的语言', 7 # 'context' : 'PHP是世界是最好的语言,这是一个不需要有疑问的问题,谁不服可以来战啊!!!' 8 # } 9 context = { 10 'books' : Book.query.order_by('-createdate').all(), 11 } 12 return render_template('home.html',**context)
html代码如下:
1 <div class="context"> 2 <div class="index index-left"></div> 3 <div class="index index-center"> 4 <div class="picture"> 5 <div class="layui-carousel" id="test1"> 6 <div carousel-item> 7 <div><img src="{{ url_for('static', filename='img/index_p1.png') }}" width="100%"/></div> 8 <div><img src="{{ url_for('static', filename='img/index_p1.png') }}" width="100%"/></div> 9 <div><img src="{{ url_for('static', filename='img/index_p1.png') }}" width="100%"/></div> 10 <div><img src="{{ url_for('static', filename='img/index_p1.png') }}" width="100%"/></div> 11 <div><img src="{{ url_for('static', filename='img/index_p1.png') }}" width="100%"/></div> 12 </div> 13 </div> 14 15 </div> 16 <br><br> 17 <div class="index-question"> 18 <div class="question-left"> 19 <ul> 20 <li> 21 <div class="item-one"> 22 <div class="author"> 23 <a class="avatar" target="_blank" href="#"> 24 <img src="http://www.bookmarkye.com/alliance.png" width="64px"> 25 </a> 26 <div class="info"> 27 <a class="nickname" target="_blank" href="#">赵苏七sukie</a> 28 <span class="time">5小时前</span> 29 </div> 30 </div> 31 <div class="title"> 32 <a target="_blank" 33 href="#">我是男性,假如我在泰坦尼克号上,我不会让妇女先走</a> 34 </div> 35 <div class="abstract"> 36 <p> 37 最近一个很火的社区出了一篇帖子,发帖者是一位男性。 38 他说,每次看泰坦尼克号上让妇女小孩先走类似的情节,我都很郁闷。凭什么男人就该死?我宁愿抓阄,公平。要不就让头等舱先走,毕竟... 39 </p> 40 </div> 41 <div class="meta"> 42 <a class="collection-tag" target="_blank" href="#">社会热点</a> 43 <a target="_blank" href="#"> 44 <span> 浏览: 4462</span> 45 </a> 46 <a target="_blank" href="#"> 47 <span> 评论: 101</span> 48 </a> 49 <span> 点赞: 134</span> 50 </div> 51 </div> 52 </li> 53 {% for book in books %} 54 <li> 55 <div class="item-one"> 56 <div class="author"> 57 <a class="avatar" target="_blank" href="#"> 58 <img src="http://www.bookmarkye.com/5.jpg" width="64px;"> 59 </a> 60 <div class="info"> 61 <a class="nickname" target="_blank" href="#">{{ book.authorClass.title }}</a> 62 <span class="time">{{ book.createdate }}</span> 63 </div> 64 </div> 65 <div class="title"> 66 <a target="_blank" 67 href="{{ url_for('poi',book_id=book.id) }}">{{ book.title }}</a> 68 </div> 69 <div class="abstract"> 70 <p> 71 {{ book.content }} 72 </p> 73 </div> 74 <div class="meta"> 75 <a class="collection-tag" target="_blank" href="#">社会热点{{ book.classificationClass.name }}</a> 76 <a target="_blank" href="#"> 77 <span> 浏览: 99999+</span> 78 </a> 79 <a target="_blank" href="#"> 80 <span> 评论: 51428</span> 81 </a> 82 <span> 点赞: 6666</span> 83 </div> 84 </div> 85 </li> 86 {% endfor %} 87 </ul> 88 </div> 89 <div class="question-right"> 90 <div class="picture"> 91 <img src="{{ url_for('static', filename='img/index_p2.png') }}" width="100%"/> 92 </div> 93 <div class="picture"> 94 <img src="{{ url_for('static', filename='img/index_p3.png') }}" width="100%"/> 95 </div> 96 </div> 97 </div> 98 </div> 99 <div class="index index-right"></div> 100 </div>
运行结果如下: