从首页问答标题到问答详情页

    1. 主PY文件写视图函数,带id参数。 
      @app.route('/detail/<question_id>')
      def detail(question_id):
          quest = 
          return render_template('detail.html', ques = quest) 
@app.route('/question_detail/<question_id>')
def question_detail(question_id):
    quest = Question.query.filter(Question.id == question_id).first()
    return render_template('question_detail.html',ques=quest)

 

    1. 首页标题的标签做带参数的链接。
            {{ url_for('detail',question_id = foo.id) }}

 <a href="{{ url_for('question_detail' ,question_id=foo.id)}}">id:{{ foo.author.username }}</a>
            <br>
            <a >标题:{{ foo.question }}</a>
            <br>
            <a >详情:{{ foo.questionDetail }}</a>
            <br>
          <span class="badge">{{ foo.creat_time }}</span>

 

    1. 在详情页将数据的显示在恰当的位置。 
      {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
 <h3>{{ ques.question }}</h3>
        <a style="font-size: 18px">{{ ques.author.username }} <span class="badge">{{ ques.creat_time }}</span></a>
        <hr>
        <a>{{ ques.questionDetail }}</a>
        <hr>
        <form action="{{ url_for('question') }}" method="post">
            <textarea  class="form-control" rows="8" id="questionDetail"></textarea>
            <br><button class="btn-default">发布</button>
        </form>
        <p>评论:</p>

 

 

 

    1. 建立评论的对象关系映射:

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

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',order_by=creat_time.desc))
    author=db.relationship('User',backref=db.backref('comments'))

 

 

    1.  尝试实现发布评论。

 

posted @ 2017-12-07 13:27  011赖颖璇  阅读(113)  评论(0)    收藏  举报