三11、從首頁問答標題到問答詳情頁

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

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

@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.首頁標題的標籤作帶參數的連接。
      {{ url_for('detail',question_id = foo.id) }}函數

<a href="{{ url_for('detail',question_id = foo.id) }}">{{ foo.title }}</a>

 

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

{{ ques.title}}
{{ ques.id  }}{{  ques.creat_time }}url

{{ ques.author.username }} 
{{ ques.detail }}spa

{% for foo in questions %}
    <ul class="new-list">
    <li>
            <a href="#">{{ foo.author.username }}</a><br>
            <a target="_self" href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a>
            <p>
            {{ foo.detail }}
            </p>
        <span class="post_item_foot">
            發佈於   {{ foo.create_time }}
        </span>
             </li>
       </ul>

 

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

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

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'))
    create_time = db.Column(db.DateTime, default=datetime.now)
    detail = db.Column(db.Text, nullable=False)
    question = db.relationship('Question',backref = db.backref('comments'))
    author = db.relationship('User',backref = db.backref('comments'))

5.嘗試實現發佈評論。對象

相關文章
相關標籤/搜索