def post(self, request, *args, **kwargs): self.object = self.get_object() form = CommentForm(object=self.object, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(self.object.get_absolute_url()) context = self.get_context_data(object=self.object, form=form) return self.render_to_response(context)
def detail(request, question_id): if request.method == "GET": question_data = get_object_or_404(Question, pk=question_id) #댓글 폼 생성 form = CommentForm() #question_data = Question.objects.get(id=question_id) # choice_data = Choice.objects.filter(question=question_id) context = { 'form': form, 'question_data': question_data, # 'choice_data':choice_data } return render(request, 'polls/detail.html', context) # return HttpResponse('question_id : {}'.format(question_id)) elif request.method == "POST": form = CommentForm(request.POST, request.FILES) if form.is_valid(): #Question 객체 찾기 question = get_object_or_404(Question, pk=question_id) #CustomUser 객체 찾기 customuser = get_object_or_404(CustomUser, pk=request.session['username']) obj = form.save(commit=False) obj.question = question obj.customuser = customuser obj.save() return HttpResponseRedirect( reverse('polls:detail', args=(question_id, )))
def comment(request): user_comment = CommentForm(data=request.POST) if request.method == 'POST': if user_comment.is_valid(): comment = user_comment.save(commit=False) comment.user = request.user poll = Poll.objects.get(pk=request.POST['pollid']) comment.poll = poll comment.save() return DetailsView(request,request.POST['pollid']) else: return DetailsView(request,request.POST['pollid']) return redirect(request.path)
def comment(request): user_comment = CommentForm(data=request.POST) if request.method == 'POST': if user_comment.is_valid(): comment = user_comment.save(commit=False) comment.user = request.user poll = Poll.objects.get(pk=request.POST['pollid']) comment.poll = poll comment.save() return DetailsView(request, request.POST['pollid']) else: return DetailsView(request, request.POST['pollid']) return redirect(request.path)
def detail(request, pk): form = CommentForm() # CommentForm을 form 안에 넣는다. c = Comment.objects.all( ) # Comment 의 모든 object를 c에 넣는다, 나중에 context로 리턴해준다. q = Question.objects.prefetch_related('choice_set').get(id=pk) # 최적화방법 if request.method == "POST": # CommentForm을 작성해서 POST로 들어오면 Pform = CommentForm( request.POST) # CommentForm에 POST로 들어온 것들을 Pform 안에 넣고 if Pform.is_valid(): # 유효성 검사를 한다 Pform.save(commit=False) # DB에 바로 저장하지 않고 Pform.comment_user = request.user.username # [작동안함] Pform.comment_date = timezone.now() # 현재 시간을 comment_date에 넣는다. Pform.question = q.question_text Pform.save() # DB에 적용시킨다. return redirect('polls:detail', pk) # 현재 페이지로 redirect 시킨다. else: return HttpResponse('유효하지 않은 데이터입니다.') # Form 데이터가 유효하지 않으면 출력 else: return render(request, 'polls/detail.html', { 'question': q, 'form': form, 'c': c }) # 함수의 끝으로 context들을 리턴
def comment_edit(request, report_id, comment_id=None): """感想の編集""" report = get_object_or_404(Report, pk=report_id) # 親の書籍を読む if comment_id: # comment_id が指定されている (修正時) comment = get_object_or_404(Comment, pk=comment_id) else: # comment_id が指定されていない (追加時) comment = Comment() if request.method == 'POST': form = CommentForm(request.POST, instance=comment) # POST された request データからフォームを作成 if form.is_valid(): # フォームのバリデーション comment = form.save(commit=False) comment.report = report # この感想の、親の書籍をセット comment.save() return redirect('polls:comment_list', report_id=report_id) else: # GET の時 form = CommentForm(instance=comment) # comment インスタンスからフォームを作成 return render(request, 'polls/comment_edit.html', dict(form=form, report_id=report_id, comment_id=comment_id))
def comment(request): comment = Comment.objects.all().order_by('-id') paginator = Paginator(comment, 5) page = request.GET.get('page') comment = paginator.get_page(page) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): c = form.save(commit=False) c.comment_date = timezone.now() c.save() return render(request, 'polls/comment.html', { 'form': form, 'comment': comment }) else: form = CommentForm() return render(request, 'polls/comment.html', { 'form': form, 'comment': comment })
def comment_new(request, map_pk): info = Info.objects.get(pk=map_pk) rate_count = 0 if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author_id = request.user.id comment.info_id = info.pk comment.rate = request.POST['rate'] for rate in comment.rate: if rate == '★': rate_count += 1 info.rate_sum += rate_count info.count += 1 info.rate_ave = round(info.rate_sum / info.count, 1) info.save() comment.save() messages.success(request, '메시지를 작성하였습니다.') return redirect('polls:maps', map_pk) # post.get_absolute_url() => post detail else: form = CommentForm() return render(request, 'poll/maps.html', {'form': form})