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

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

  1. 主PY文件写视图函数,带id参数。 
    @app.route('/detail/<question_id>')
    def detail(question_id):
        quest = 
        return render_template('detail.html', ques = quest) 
    @app.route('/detail/<question_id>')
    @log_re
    def detail(question_id):
        quest=Question.query.filter(Question.id==question_id).first()
        return render_template("detail.html",ques=quest)
        return question_id
  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="../static/css/detail.css">
    {% endblock %}
    {% block main %}
        <ul class="list">
            <p class="title">{{ ques.title }}</p>
            <a href="usern">{{ ques.author.username }}</a>
            <p class="answer" style="text-indent: 18px">{{ ques.detail }}</p>
            <span class="badge">{{ ques.creat_time }}</span>
            <a href="a">回复♥</a>
            <a href="b">送礼</a>
            <div>
              <input id="com" name="comment" type="text"   placeholder="我也说一句..." >
            <form action="{{ url_for('comment') }}" method="post">
           <button>发送</button>
    
       </form>
        </div></ul>
    
    {% endblock %}
  4. 建立评论的对象关系映射:

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

    @app.route('/comment/',methods=['POST'])
    @log_re
    def comment():
            user = User.query.filter(User.username == session.get('user')).first()
            author_id = user.id
            question = Question.query.filter(Question.author_id == author_id ).first()
            question_id = question.id
            comment = Comment(author_id=author_id,question_id=question_id,detail=detail)
            db.session.add(comment)  # 保存到数据库
            db.session.commit()  # 提交
            return redirect(url_for('index'))

     

  5.  尝试实现发布评论。

     

     

posted @ 2017-12-07 13:59 林丹宜 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值