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

 

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

  3. 在详情页将数据的显示在恰当的位置。 
    {{ ques.title}}
    {{ ques.id  }}{{  ques.creat_time }}
    {{ ques.author.username }} 
    {{ ques.detail }}
  4. 建立评论的对象关系映射:

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

  5.  尝试实现发布评论。

 

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)
    creatTime=db.Column(db.DateTime,default=datetime.now)
    question=db.relationship('Question',backref=db.backref('comments',order_by=creatTime.desc))
    author=db.relationship('User',backref=db.backref('comments'))
#db.create_all()

@app.route('/')
def home():
    context={
        'question': Question.query.all()
    }
    return render_template('shouye.html',**context)

@app.route('/question_detail/<question_id>')
def question_detail(question_id):
    question = Question.query.filter(Question.id == question_id).first()
    return render_template('question_detail.html',question=question)

@app.route('/comment/',methods=['GET','POST'])
# @loginFirst
def comment():
    if request.method == 'GET':
        return render_template('question_detail.html')
    else:
        detail = request.form.get('detail')
        author_id =User.query.filter(User.username == session.get('user')).first().id
        #question_id=Question.query.filter(Question.authorID ==author_id).first().id
        #question=Question.query.filter(Question.authorID == session.get('question')).first()
        #user = User.query.filter(User.username == session.get('user')).first()
        comments = Comment(detail=detail,author_id=author_id,question_id=question_id)
        #comments.question=question
        #comments.author = user
        db.session.add(comments)
        db.session.commit()
        return redirect(url_for('home'))if __name__ == '__main__':
    app.run(debug=True)
{% extends'base.html' %}
{% block title %}
    Home
{% endblock %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css/question_detail.css')}}" type="text/css">
{% endblock %}
{% block main %}
<body>
<div class="detail">
    <div class="detail_left">

    <h2>{{ question.title }}</h2>
    <a class="username">{{ question.author.username }}</a>
    <span class="badge">{{ question.creatTime }}</span>
    <hr>
    <a style="white-space: pre-wrap"  >{{ question.detail }}</a>
    <hr>
    <form action="{{ url_for('comment') }}" method="post">
    <textarea name='detail' class="form-control" rows="6" id="questionDetail"></textarea>
    <br><button class="btn-default">发布</button>
     </form>
    <p>评论:({{ question.comments|length }})</p>
    <ul class="comment">
        {% for foo in question.comments %}
        <span class="icon" aria-hidden="true"><img src="../static/images/icon.jpg"></span>
        <a href="#" class="name">{{ foo.author.username }}</a>
         <span class="badge2">{{ foo.creatTime }}</span>
           <br>
        <p class="neirong">{{ foo.detail }}</p>

        {% endfor %}
    </ul>


</div>
</div>
</body>
{% endblock %}

posted @ 2017-12-06 22:49  078刘凯敏  阅读(137)  评论(0编辑  收藏  举报