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

    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) }}
      {% extends 'base.html' %}
      {% block title %}
          首页
      {% endblock %}
      {% block head %}
          <link type="text/css" rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}">
      
      {% endblock %}
      {% block main %}
      
      
             <ul class="list1" >
             <img src="../static/images/myself.jpg" width="60" height="60">
                 {% for foo in questions %}
                     <li class="list-group-item">
                        <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                     {% if username %}
                         <li class="zero"><a href="{{ url_for('detail',question_id=foo.id) }}">{{ username }}</a></li>
                     {% endif %}
                     <a class="#"></a>
                         <br>
                     <span class="badge">{{ foo.creat_time }}</span>
                     <li class="one" a href="#">{{ foo.title }}</li>
                     <li class="two"><p style="text-indent: 18px">{{ foo.detail }}</p></li>
                     </li>
                 {% endfor %}
             </ul>
      
      {% endblock %}
    3. 在详情页将数据的显示在恰当的位置。 
      {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
      {% extends 'base.html' %}
      
      {% block title %}问答{% endblock %}
      {% block head %}
          <link type="text/css" rel="stylesheet" href="{{ url_for('static',filename='css/label.css') }}">
          <script src="{{ url_for('static',filename='js/label.js') }}"></script>
      {% endblock %}
      
      {% block main %}
         <div class="label">
          <h1 style="color:pink",align="center"> 发布问答</h1>
              <div class="q">
                  <label for="question">问题</label>
                  <textarea id="question" cols="50" rows="1"></textarea>
              </div>
              <div class="form-group">
                  <label for="questionDetail">内容</label>
                  <textarea class="form-control" id="questionDetail" cols="50" rows="5"></textarea>
              </div>
             <br>
              <div class="input-area">
                  <button onclick="fnQuestion">发布问答</button>
              </div>
          </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'))
          sent_id = db.Column(db.Integer, db.ForeignKey('sent.id'))
          creat_time = db.Column(db.DateTime, default=datetime.now)
          detail = db.Column(db.TEXT, nullable=False)
          sent = db.relationship('Sent', backref=db.backref('comment'))
          author = db.relationship('User', backref=db.backref('comment'))
    5.  尝试实现发布评论

posted @ 2017-12-07 19:02  073徐英杰  阅读(132)  评论(0编辑  收藏  举报