1. 程式人生 > >首頁列表顯示全部問答,完成問答詳情頁布局。

首頁列表顯示全部問答,完成問答詳情頁布局。

includes mit tail style cal clu sub pull static

  1. 首頁首頁列表顯示全部問答:
    1. 將數據庫查詢結果傳遞到前端頁面 Question.query.all()
    2. 前端頁面循環顯示整個列表。
    3. 問答排序

py文件:

@app.route(/)
def index():
    context = {
     questions:Question.query.all()
    }
    return render_template("index.html",**context)

首頁html:

{% extends‘myweb.html‘ %}
{% block indextitle %}首頁{% endblock %}




{% block indexhead %}
    
<link rel="stylesheet" type="text/css" href="../static/css/component.css"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="../static/js/login.js"></script> {% endblock %} {% block indexbody %} <div class="container"
> <div class="row clearfix"> <div class="col-md-4 column"> <div class="jumbotron well"> <h1> Hello, world! </h1> <p> This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.
</p> <p> <a class="btn btn-primary btn-large" href="#">Learn more</a> </p> </div> </div> <div class="col-md-8 column" style="background: whitesmoke"> <p style="color: #FFFFFF">`{{ username }}context</p> <ul> {% for foo in questions %} <li> <a href="#">{{ foo.author.username }}</a> <br> <a href="{{ url_for(‘detail‘,question_id=foo.id) }}">{{ foo.title }}</a> <br> <p>內容:{{ foo.detail }}</p> <span>評論數: 100000++++</span> <span class="badge pull-right">{{ foo.create_time }}</span> <hr> </li> {% endfor %} </ul> </div> </div> </div> {% endblock %}

  1. 完成問答詳情頁布局:
    1. 包含問答的全部信息
    2. 評論區
    3. 以往評論列表顯示區。
  2. 在首頁點擊問答標題,鏈接到相應詳情頁。

py文件:

@app.route(/detail/<question_id>)
def detail(question_id):
    quest = Question.query.filter(Question.id==question_id).first()
    return  render_template(detail.html,quest=quest)

html文件:

{% extends ‘myweb.html‘ %}
{% block detailtitle %}問答詳情{% endblock %}
{% block detailhead %}
    <link rel="stylesheet" type="text/css" href="../static/css/component.css"/>
    <script src="../static/js/regist.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
{% endblock %}

{% block detailbody %}
    <div class="col-md-2 column "></div>
    <div class="col-md-8 column ">
        <ul class="list-unstyled">
            <li>
                <h2 href="#" class="text-center">{{ quest.title }}</h2>
                <br>
                <p class="text-center">
                    <a href="#">
                        <small>{{ quest.author.username }}</small>
                    </a>&nbsp&nbsp&nbsp
                    <span class="pull-center"><small>{{ quest.create_time }}</small></span>
                </p>
                <p>{{ quest.detail }}</p>
                <form>
                    <div class="form-group">
                    <textarea name="comment" class="form-control" rows="5" id="comment"
                              placeholder="請輸入評論"></textarea>
                    </div>
                    <button type="submit" class="btn btn-default" style="margin-left:48% ">發送</button>
                </form>
            </li>
        </ul>
    </div>
    <div class="col-md-2 column "></div>
{% endblock %}

運行截圖:

1.首頁

技術分享圖片

2.詳情頁:

技術分享圖片

首頁列表顯示全部問答,完成問答詳情頁布局。