@app.route('/comments/',methods=['POST']) @loginFirst def comments(): comment=request.form.get('new_comment') questions_id =request.form.get('questions_id') author_id = User.query.filter(User.username == session.get('user')).first().id comments = Comment(detail=comment, author_id=author_id, questions_id=questions_id) db.session.add(comments) db.session.commit() # 提交 return redirect(url_for('detail',questions_id=questions_id))
{% extends'base.html' %} {% blook title %}問答詳情{% endblock %} {% block main %} <div class="page-header" <h3>{{ ques.title }}<br><small>{{ ques.author.username }}<span class="badge">{{ ques.creat_time}} </span></small></h3> </div> <p class="lead">{{ ques.detail }}</p> <hr> <form action="{{ url_for('comment') }}"method="''post"style=""></form> <div class="form-group" style="align-items: "> <textarea name="new_comment" class="form-control" rows="3" id="new-comment" placeholder="write your comment"></textarea>
<input type="hidden" name="questions_id" value="{{ questions.id }}">
</div>
<button type="submit" class="btn btn-default">發送</button> </form>
<h4> 評論:({{ ques.comments|length }})</h4>
<ul class="list-group" style="align-items:"> </ul>
{% endblock %}