码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-07 21:09:57      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:数据   list   ssi   lis   标题   hicon   mode   filter   rect   

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

{% block main %}


       <ul class="list1" >
           {% 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 }}
{% 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>
        <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‘))

 

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

标签:数据   list   ssi   lis   标题   hicon   mode   filter   rect   

原文地址:http://www.cnblogs.com/0058kyle/p/8000482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!