從首頁問答標題到問答詳情頁

    1. 主PY文件寫視圖函數,帶id參數。 
      @app.route('/detail/<question_id>')
      def detail(question_id):
          quest = 
          return render_template('detail.html', ques = quest)
    2. 首頁標題的標籤作帶參數的連接。
            {{ url_for('detail',question_id = foo.id) }}

    3. 在詳情頁將數據的顯示在恰當的位置。 
      {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
    4. 創建評論的對象關係映射:css

      class Comment(db.Model):
          __tablename__='comment'html

    5.  嘗試實現發佈評論。app

 1.主PY文件寫視圖函數,帶id參數。函數

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

2.首頁標題的標籤作帶參數的連接。post

{% extends'base.html' %}
{% block title %}
    首頁
{% endblock %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}" type="text/css">
{% endblock %}
{% block main %}
    <body id="myBody">
    <div class="indexone">
        <ul class="list-group">
            {% for foo in questions %}
                <li class="list-group-item">
                    <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                     <a href="#" style="font-family: 幼圓">{{ foo.author.username }}</a><br>
                    <a href="{{ url_for('detail',question_id = foo.id) }}" style="font-family: 幼圓">{{ foo.title }}</a><br>
                    <span class="badge" style="font-family: 幼圓">{{ foo.creat_time }}</span><br>
                    <p style="font-family: 幼圓;color: #002D54;">{{ foo.detail }}</p>
                    <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                <div class="meta">
                            <a class="collection-tag">
                                <span style="font-family: 幼圓">社會熱點</span>
                            </a>
                            <a class="collection-tag">
                                <span style="font-family: 幼圓"> 瀏覽: </span><span
                                    style="font-family: 幼圓;color: red"> 555 </span>
                            </a>
                            <a class="collection-tag">
                                <span style="font-family: 幼圓"> 評論: </span><span
                                    style="font-family: 幼圓;color: red"> 890 </span>
                            </a>
                            <span style="font-family: 幼圓"> 點贊: </span><span
                                style="font-family: 幼圓;color: red"> 1234 </span>
                        </div>
                </li>
            {% endfor %}
        </ul>
    </div>

    </body>
    </html>
{% endblock %}

 

3.在詳情頁將數據的顯示在恰當的位置。 url

{% extends'base.html' %}
{% block title %}
    問答詳情
{% endblock %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css/detail.css') }}" type="text/css">
{% endblock %}
{% block main %}
    <body id="myBody">
    <div class="total">
        <div class="page-header" style="text-align: left">
            <h3>{{ ques.title }}<br>
                <small>做者:{{ ques.author.username }}&nbsp&nbsp&nbsp<span class="badge">{{ ques.creat_time }}</span>
                </small>
            </h3>
        </div>
        <hr>
        <p class="lead">{{ ques.detail }}</p>
        <hr>
        <form method="post" action="{{ url_for('comment') }}">
            <div class="title" style="text-align: left">
                <textarea class="form-control" rows="3" placeholder="請輸入要發佈的內容" name="detail"></textarea>
            </div>
            <button type="submit" id="fabu">發送</button>
            <h3 style="text-align: left">評論:({{ ques.comments|length }})</h3>
            <div class="basic_box" style="padding-bottom: 50px;">
                <ul class="list-group" style="margin-bottom: 10px">
                    {% for foo in comments %}
                        <li class="list-group-item" style="width: 800px">
                            <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                            <br>
                            <a href="#">{{ foo.author_id }}</a>
                            <span class="badge">評論時間:{{ foo.creat_time }}</span>
                            <p>{{ foo.detail }}
                            </p>
                        </li>
                    {% endfor %}
                 </ul>
            </div>
        </form>
    </div>
    </body>
    </html>
{% endblock %}

4.創建評論的對象關係映射:spa

 5.嘗試實現發佈評論。code

class Comment(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
    detail = db.Column(db.Text, nullable=False)
    creat_time = db.Column(db.DateTime, default=datetime.now)
    question = db.relationship('Question', backref=db.backref('comments'))
    author = db.relationship('User', backref=db.backref('comments'))
相關文章
相關標籤/搜索