視圖函數:css
@app.route('/xiangqing/<question_id>') def xiangqing(question_id): quest = Question.query.filter(Question.id == question_id).first() return render_template('xiangqing.html',ques=quest) @app.route('/comment/',methods=['POST']) @denglufirst def comment(): comment=request.form.get('pinglun') ques_id=request.form.get('question_id') user = User.query.filter(User.username == session.get('user')).first() auth_id=user.id comm=Comment(author_id=auth_id,question_id=ques_id,detail=comment) db.session.add(comm) db.session.commit() return redirect(url_for('xiangqing',question_id=ques_id))
詳情頁面htmlhtml
{% extends'shouye.html' %} {% block title %} 問答詳情頁 {% endblock %} {% block jiemian %} <body bgcolor="antiquewhite"><link rel="stylesheet" type="text/css" href="../static/css/cc.css"> <div class="d1"> <form action="{{ url_for('comment') }}" method="post"> <p><span class="timu">{{ ques.title }}<br></span> <h1 class="neir" >{{ ques.detail }}</h1></p> <a href="#" class="a"><img class="people" src="{{ url_for('static',filename='image/people.png') }}">{{ ques.author.username }}</a> <hr class="hr"> <textarea class="pinglun" name='pinglun'></textarea> <input name="question_id" type="hidden" value="{{ ques.id }}"> <br> <input type="submit" value="發佈"style="width:65px;height:25px;font-size:15px"> <p>評論:({{ ques.comments|length }})</p> <table border=1 > {% for foo in ques.comments %} <tr><td> <img class="people" src="{{ url_for('static',filename='image/people.png') }}"> <a href="#" class="name">{{ foo.author.username }}</a> <span class="time" >{{ foo.creat_time }}</span><br> <p class="nr">{{ foo.detail }}</p></td></tr> {% endfor %} </table> </form> </div> </body> {% endblock %} </html>