def comment_create_answer(request, answer_id): answer = get_object_or_404(Answer, pk=answer_id) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.create_date = timezone.now() comment.answer = answer comment.save() return redirect('pybo:detail', question_id=comment.answer.question.id) else: form = CommentForm() context = {'form': form} return render(request, 'pybo/comment_form.html', context)
def comment_create_question(request, question_id): question = get_object_or_404(Question, pk=question_id) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.create_date = timezone.now() comment.question = question comment.save() # return redirect('pybo:detail', question_id=question.id) return redirect( f'{resolve_url("pybo:detail", question_id=comment.question.id)}#comment_{comment.id}' ) else: form = CommentForm() context = {'form': form} return render(request, 'pybo/comment_form.html', context)
def comment_modify_answer(request, comment_id): comment = get_object_or_404(Comment, pk=comment_id) if request.user != comment.author: messages.error(request, '댓글 수정 권한이 없습니다') return redirect('pybo:detail', question_id=comment.answer.question.id) if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.modify_date = timezone.now() comment.save() return redirect('pybo:detail', question_id=comment.answer.question.id) else: form = CommentForm(instance=comment) context = {'form': form} return render(request, 'pybo/comment_form.html', context)
def comment_create_question(request, question_id): question = get_object_or_404(Question, pk=question_id) if request.user != question.author: messages.error(request, '생성 권한이 없습니다.') if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.create_date = timezone.now() comment.question = question comment.save() return redirect('pybo:detail', question_id=question.id) else: form = CommentForm() context = {'form': form} return render(request, 'pybo/comment_form.html', context)
def comment_modify_question(request, comment_id): """ Comment Edit """ comment = get_object_or_404(Comment, pk=comment_id) if request.user != comment.author: messages.error(request, 'No Permission') return redirect('pybo:detail', question_id=comment.question.id) if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.modify_date = timezone.now() comment.save() return redirect('{}#comment_{}'.format( resolve_url('pybo:detail', question_id=comment.question.id), comment.id)) else: form = CommentForm(instance=comment) context = {'form': form} return render(request, 'pybo/comment_form.html', context)