점프 투 플라스크 207~8 페이지, 답변 댓글 설정시 detail페이지에 접근이 안되는 현상
점프 투 플라스크 207~8 페이지 진행중입니다.
<!-- 답변 댓글 Start -->
{% if answer.comment_set|length > 0 %}
<div class="mt-3">
{% for comment in answer.comment_set %}
<div class="comment py-2 text-muted">
<span style="white-space: pre-line;">{{ comment.content }}</span>
<span>
- {{ comment.user.username }}, {{ comment.create_date|datetime }}
{% if comment.modify_date %}
(수정일시:{{ comment.modify_date|datetime }})
{% endif %}
</span>
{% if g.user == comment.user %}
<a href="{{ url_for('comment.modify_answer', comment_id=comment.id) }}" class="small">수정</a>,
<a href="#" class="small delete"
data-uri="{{ url_for('comment.delete_answer', comment_id=comment.id) }}">삭제</a>
{% endif %}
</div>
{% endfor %}
</div>
{% endif %}
<div>
<a href="{{ url_for('comment.create_answer', answer_id=answer.id) }}" class="small">
<small>댓글 추가...</small>
</a>
</div>
<!-- 답변 댓글 End -->
위와 같이 코드 작성하고 detail 페이지 접근시 아래와 같은 메세지를 받습니다:
werkzeug.routing.BuildError: Could not build url for endpoint 'comment.create_answer' with values ['answer_id']. Did you forget to specify values ['comment_id']?
그리고 혹시 모를 단서를 추가하자면,
<a href="{{ url_for('comment.create_answer', answer_id=answer.id) }}" class="small">
<small>댓글 추가...</small>
</a>
위 코드의 answer_id를 comment_id로 바꾸면 detail페이지까지는 진행이 되는데, 답변의 '댓글 추가...'를 클릭하면 아래와 같은 메시지를 받습니다:
TypeError: create_answer() got an unexpected keyword argument 'comment_id'
어디를 손봐야 될지 모르겠습니다ㅠ 도와주세요
thebjko 님 2074
2021년 4월 10일 3:13 오후
2개의 답변이 있습니다. 1 / 1 Page
comment_views.py의 create_answer 함수의 매개변수가 잘못된게 아닐까요?
작성하신 comment_views.py 파일을 봐야 알수 있을거 같습니다.
박응용 님
2021년 4월 10일 4:50 오후
박응용 선생님, comment_views.py 코드 올립니다.
from datetime import datetime
from flask import Blueprint, url_for, request, render_template, g, flash
from werkzeug.utils import redirect
from pybo import db
from pybo.forms import CommentForm
from pybo.models import Question, Comment, Answer
from pybo.views.auth_views import login_required
bp = Blueprint('comment', __name__, url_prefix='/comment')
@bp.route('/create/question/<int:question_id>', methods=('GET', 'POST'))
@login_required
def create_question(question_id):
form = CommentForm()
question = Question.query.get_or_404(question_id)
if request.method == 'POST' and form.validate_on_submit():
comment = Comment(user=g.user, content=form.content.data, create_date=datetime.now(), question=question)
db.session.add(comment)
db.session.commit()
return redirect(url_for('question.detail', question_id=question_id))
return render_template('comment/comment_form.html', form=form)
@bp.route('/modify/question/<int:comment_id>', methods=('GET', 'POST'))
@login_required
def modify_question(comment_id):
comment = Comment.query.get_or_404(comment_id)
if g.user != comment.user:
flash('수정권한이 없습니다.')
return redirect(url_for('question.detail', question_id=comment.question.id))
if request.method == 'POST':
form = CommentForm()
if form.validate_on_submit():
form.populate_obj(comment)
comment.modify_date = datetime.now()
db.session.commit()
return redirect(url_for('question.detail', question_id=comment.question.id))
else:
form = CommentForm(obj=comment)
return render_template('comment/comment_form.html', form=form)
@bp.route('/delete/question/<int:comment_id>')
@login_required
def delete_question(comment_id):
comment = Comment.query.get_or_404(comment_id)
question_id = comment.question.id
if g.user != comment.user:
flash('삭제권한이 없습니다.')
return redirect(url_for('question.detail', question_id=question_id))
db.session.delete(comment)
db.session.commit()
return redirect(url_for('question.detail', question_id=question_id))
@bp.route('create/answer/<int:comment_id>', methods=('GET', 'POST'))
@login_required
def create_answer(answer_id):
form = CommentForm()
answer = Answer.query.get_or_404(answer_id)
if request.method == 'POST' and form.validate_on_submit():
comment = Comment(user=g.user, content=form.content.data, create_date=datetime.now(), answer=answer)
db.session.add(comment)
db.session.commit()
return redirect(url_for('question.detail', question_id=answer.question.id))
return render_template('comment/comment_form.html', form=form)
@bp.route('modify/answer/<int:comment_id>', methods=('GET', 'POST'))
@login_required
def modify_answer(comment_id):
comment = Comment.query.get_or_404(comment_id)
if g.user != comment.user:
flash('수정권한이 없습니다.')
return redirect(url_for('question.detail', question_id=comment.answer.id))
if request.method == 'POST':
form = CommentForm()
if form.validate_on_submit():
form.populate_obj(comment)
comment.modify_date = datetime.now()
db.session.commit()
return redirect(url_for('question.detail', question_id=comment.answer.question.id))
else:
form = CommentForm(obj=comment)
return render_template('comment/comment_form.html', form=form)
@bp.route('/delete/answer/<int:comment_id>')
@login_required
def delete_answer(comment_id):
comment = Comment.query.get_or_404(comment_id)
question_id = comment.answer.question.id
if g.user != comment.user:
flash('삭제권한이 없습니다.')
return redirect(url_for('question.detail', question_id=question_id))
db.session.delete(comment)
db.session.commit()
return redirect(url_for('question.detail', question_id=question_id))
thebjko 님
M 2021년 4월 12일 2:36 오후