flask 답변 페이징 질문
from datetime import datetime
from flask import Blueprint, url_for,request, render_template, g, flash
from pybo.views.auth_views import login_required
from werkzeug.utils import redirect
from pybo import db
from ..forms import AnswerForm
from ..models import Question, Answer
bp = Blueprint('answer', __name__, url_prefix='/answer')
@bp.route('/detail/')
def _detail():
page = request.args.get('page', type=int, default=1)
question_detail = Answer.query.order_by(Answer.create_date.desc())
question_detail = question_detail.paginate(page, per_page=10)
return render_template('quetion/quesiton_detail.html', question_detail=question_detail)
@bp.route('/create/<int:question_id>', methods=('POST',))#post방식으로 전달받음
@login_required
def create(question_id):
form =AnswerForm()
question = Question.query.get_or_404(question_id)
if form.validate_on_submit():
content = request.form['content']#post방식으로 전송된 form을 받음
answer = Answer(content=content, create_date=datetime.now(), user=g.user)
question.answer_set.append(answer)#answer_set은 질문의 답변을 의미 질문의 대한 답변 추가
db.session.commit()#데이터베이스에 추가
return redirect('{}#answer_{}'.format(
url_for('question.detail', question_id=answer.question.id), answer.id))#답변생성후 상세조회 페이지로 이동
return render_template('question/question_detail.html', question=question, form=form)
@bp.route('/modify/<int:answer_id>', methods=('GET', 'POST'))
@login_required
def modify(answer_id):
answer = Answer.query.get_or_404(answer_id)
if g.user != answer.user:
flash('수정권한이 없습니다')
return redirect(url_for('question.detail', question_id=answer.question.id))
if request.method == "POST":
form = AnswerForm()
if form.validate_on_submit():
form.populate_obj(answer)
answer.modify_date = datetime.now() # 수정일시 저장
db.session.commit()
return redirect('{}#answer_{}'.format(
url_for('question.detail', question_id=answer.question.id), answer.id))
else:
form = AnswerForm(obj=answer)
return render_template('answer/answer_form.html', answer=answer, form=form)
@bp.route('/delete/<int:answer_id>')
@login_required
def delete(answer_id):
answer = Answer.query.get_or_404(answer_id)
question_id = answer.question_id
if g.user != answer.user:
flash('삭제권한이 없습니다')
else:
db.session.delete(answer)
db.session.commit()
return redirect(url_for('question.detail', question_id=question_id))
answer_views에 detail 함수를 추가하여 question.detail에 {% for answer in question.answer_set.items %} 이런식으로 했는데 답변이 아예보이지 않네요. 답변 페이징은 어떤식으로 해야하나요??
sin99027 님 815
2021년 4월 4일 12:27 오전