12.6

  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) }}
    {% block main %}
        <!--<img src="{{ url_for }}">-->
        <ul class="list-group">
        {% for foo in post %}
           <li class="list-group" >
               <span class="badge" aria-hidden="true"></span>
               <a href="#">{{ foo.author.username }}</a>
               <br>
               <a href="{{url_for('detail',post_id=foo.id)}}">{{foo.title}}</a>
               <span class="badge">{{ foo.creat_time }}</span>
               <p style="">{{ foo.detail }}</p>
    
           </li>
        {% endfor %}
        </ul>

     

  3. 在详情页将数据的显示在恰当的位置。 
    {{ ques.title}}
    {{ ques.id  }}{{  ques.creat_time }}
    {{ ques.author.username }} 
    {{ ques.detail }}
    1. {% extends 'base.html' %}
      {% block title %}
          问答详情
      {% endblock %}
      {% block main %}
          <div class="box">
              <h1 href="#" >{{ ques.title }}</h1><small> {{ ques.author.username }}
      <span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span></small> <hr> <p>{{ ques.detail }}</p> <hr> <form> <div><textarea class="form-control" id="comment" rows="3" style="margin-left: 1%" name="comment"
      placeholder="write your comment"></textarea><br></div> <button type="submit" >发送</button> </form> <h2>评论:</h2> <ul class="list-group"> <li class="list-group-item"> <img style="width: 50px" src="{{ url_for('static',filename='css/zhaopian.jpg') }}" alt="64"> <a href="#"></a><br> <p style="align-content: center"></p> <span class="badge" style="margin-left: 60%"></span> <p style="margin-left: 25%"></p><br> </li> </ul> </div> {% endblock %}

       

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

      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'))
           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')

       

posted @ 2017-12-06 15:39  037吴宜珊  阅读(152)  评论(0)    收藏  举报