def post(self, request, pk): form = CommentForm(request.POST) if form.is_valid(): self.object = form.save(commit=False) self.object.user = get_object_or_404(User, pk=self.request.user.id) self.object.tweet = get_object_or_404(Tweet, pk=pk) self.object = form.save() return HttpResponseRedirect(reverse('tweet-detail', args=(pk, )))
def create_comment_view(request): if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): article_id = form.cleaned_data['article_id'] article = get_article_by_id(article_id) if not article: return JsonFailResponse({'msg': 'article_id不存在'}) form.save() return JsonSuccessResponse({})
def create_comment(request): http_referer = request.META.get('HTTP_REFERER') if not http_referer: return HttpResponseForbidden() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): article_id = form.cleaned_data['article_id'] path = parse.urlparse(http_referer).path a_id = path.split('/')[-1] if int(article_id) != int(a_id): return HttpResponseForbidden() anchor = request.POST.get('err_anchor', '') success, msg = is_valid_comment(form) if not success: return HttpResponseRedirect( reverse('app:detail_article', args=(article_id,)) + '?form_error=' + msg + anchor) # article_meta.comment_num += 1 # article_meta.save() comment = form.save() anchor = request.POST.get('success_anchor', '') anchor += str(comment.id) return HttpResponseRedirect(reverse('app:detail_article', args=(article_id,)) + anchor) else: anchor = request.POST.get('err_anchor', '') article_id = form.cleaned_data['article_id'] return HttpResponseRedirect( reverse('app:detail_article', args=(article_id,)) + '?form_error=' + '验证码错误' + anchor)
def comment(request, event_id): ''' Create new Comment. ''' context = RequestContext(request) now = timezone.now() event = get_object_or_404(Event, id=event_id) profile = UserProfile.objects.get(user_id=request.user.id) if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): note = comment_form.save(commit=False) note.author = profile note.event = event note.created = now note.save() return HttpResponseRedirect('/event/%d' % event.id) else: comment_form = CommentForm() return render(request, 'app/comment.html', {'event': event, 'profile': profile, 'comment_form': comment_form} )
def event_detail(request, event_id): ''' Get and return all details for the Event with event_id = event_id. ''' now = timezone.now() event = get_object_or_404(Event, id=event_id) profile = UserProfile.objects.get(user_id=request.user.id) participants = all_info_many_profiles(event.participants.all()) owners = all_info_many_profiles(event.owners.all()) comments = Comment.objects.filter(event=event_id) editform = EditEventForm(instance=event) event_over = event.end_time <= now in_progress = event.event_time <= now and now <= event.end_time if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): note = comment_form.save(commit=False) note.author = profile note.event = event note.created = now note.save() return HttpResponseRedirect('/event/%d' % event.id) else: comment_form = CommentForm() context = {'event': event, 'participants': participants, 'owners': owners, 'editform': editform, 'comments': comments, 'comment_form': comment_form, 'event_over': event_over, 'in_progress': in_progress} return render(request, 'app/event_detail.html', context)
def post(self, request, *args, **kwargs): form = CommentForm(request.POST) if form.is_valid(): comment = form.save() comment.save() messages = Message.objects.all() cache.set('messages', pickle.dumps(messages)) return HttpResponseRedirect(reverse_lazy('app:message_list')) return HttpResponseRedirect(reverse_lazy('app:message_list'))
def test_save_creates_associated_comment(self): doc = Document.objects.create() form = CommentForm(doc, {'comment': 'Hello World'}) form.full_clean() comment = form.save() self.assertEqual(doc.comment_set.count(), 1) self.assertEqual(Comment.objects.all().count(), 1) self.assertEqual(comment.document, doc)
def add_message(request, team_id): if request.user.is_authenticated: team = Team.objects.get(pk=team_id) user = request.user form = CommentForm(request.POST) comment = form.save(commit=False) comment.author = user comment.save() team.messages.add(comment) team.save() return redirect('/home/' + str(team_id))
def create_comment(request): if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): article_id = form.cleaned_data['article_id'] article = get_article_by_id(article_id) if not article: msg = '错误id' return HttpResponseRedirect( reverse('app:detail_article', args=(article_id, )) + '?form_error=' + msg + '#comment') # article_meta.comment_num += 1 # article_meta.save() form.save() return HttpResponseRedirect( reverse('app:detail_article', args=(article_id, )) + '#comment')
def post(request, pk): category_count = get_category_count() most_recent = Post.objects.order_by('-timestamp')[:3] post = get_object_or_404(Post, pk=pk) if request.user.is_authenticated: PostView.objects.get_or_create(user=request.user, post=post) form = CommentForm(request.POST or None) if request.method == 'POST': if form.is_valid(): form.instance.user = request.user form.instance.post = post form.save() return redirect(reverse('post-detail', kwargs={'pk': post.pk})) context = { 'form': form, 'post': post, 'most_recent': most_recent, 'category_count': category_count } return render(request, 'post.html', context)
def add_comment(request, id): if request.user.is_authenticated: todo = TODO.objects.get(pk=id) user = request.user form = CommentForm(request.POST) comment = form.save(commit=False) comment.author = user comment.save() todo.comments.add(comment) todo.save() return redirect('/todo/' + str(id) + '/comments')
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})
def add_private_message(request, user_id, second_user_id, conversation_id): if request.user.is_authenticated: conversation = PrivateConversation.objects.get(pk=conversation_id) user = request.user form = CommentForm(request.POST) comment = form.save(commit=False) comment.author = user comment.save() conversation.messages.add(comment) conversation.save() return redirect('/private_conversation/' + str(user_id) + '/' + str(second_user_id))
def add_comment(request, slug): # if request.user.is_anonymous: # return HttpResponseRedirect(reverse('user_login')) if request.method == 'GET': return HttpResponseBadRequest() product = get_object_or_404(Product, slug=slug) form = CommentForm(data=request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.product = product new_comment.user = request.user new_comment.save() messages.success(request, 'Tebrikler yorumunuz oluşturuldu.') return HttpResponseRedirect(product.get_absolute_url())
def blog_single(request, id): product = Product.objects.filter(id=id).first() comments = Comment.objects.filter(product_id=id) if request.method == 'POST': commentForm = CommentForm(request.POST) if commentForm.is_valid(): comment = commentForm.save(commit=False) comment.user = request.user comment.product = product comment.save() else: commentForm = CommentForm() return render(request, "app/blog_single.html", {"product": product, 'commentForm': commentForm, 'comments': comments})
def lois_view_detail(request, id): """ Vue detail de la lois """ lois = Lois.objects.get(id=id) comments = Comment.objects.filter(lois=id) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form = form.save(commit=False) form.comment_title = request.user form.lois = lois form.comment = request.POST['comment'] form.save() return redirect('app:lois-view', id) else: form = CommentForm() context = { 'lois': lois, 'comments': comments, 'form': form, } template_name = 'pages/lois-view.html' return render(request, template_name, context)
def article_comment(request, post_id, parent_comment_id=None): # 这个函数的作用是当获取的文章(Article)存在时,则获取;否则返回 404 页面给用户。 article = get_object_or_404(Article, id=post_id) u_id = request.session.get('id', '') if request.method == 'POST': form = CommentForm(request.POST) # 当调用 form.is_valid() 方法时,Django 自动帮我们检查表单的数据是否符合格式要求。 if form.is_valid(): # 检查到数据是合法的,调用表单的 save 方法保存数据到数据库, # commit=False 的作用是仅仅利用表单的数据生成 Comment 模型类的实例,但还不保存评论数据到数据库。 new_comment = form.save(commit=False) new_comment.article = article # new_comment.user = request.user new_comment.user = User.objects.get(id=u_id) new_comment.nickname = new_comment.user.nickname # 二级回复 if parent_comment_id: print(parent_comment_id) parent_comment = ArticleComment.objects.get(id=parent_comment_id) # 如果回复层级超过二层,强制转化成二级 new_comment.parent_id = parent_comment.get_root().id # 被回复的人 new_comment.reply_to = parent_comment.user new_comment.nickname = User.objects.get(id=u_id).nickname new_comment.save() return HttpResponse('200 OK') new_comment.save() return redirect('/blog/blog_detail/{}/'.format(article.id)) else: return HttpResponse('提交表单有问题') elif request.method == 'GET': form = CommentForm() comment_list = article.articlecomment_set.all() context = { 'article_id': post_id, 'form': form, 'parent_comment_id': parent_comment_id, } print('12232321231321321') return render(request, 'blog/reply.html', context=context)
def post_detail(request, pk): post = get_object_or_404(Post, pk=pk) comments = Comment.objects.filter(post=post).order_by('-created_date') comment_form = CommentForm() if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.user = request.user comment.save() return redirect(request.path) page = pagination(request, comments, 5) return render(request, 'post_detail.html', { 'post': post, 'comment_form': comment_form, 'page': page })
def add_comment(request, article_id): """Add a new comment.""" template = "comment.html" if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): temp = form.save(commit=False) temp.user = request.user temp.date = datetime.datetime.now() temp.article_id = article_id temp.save() return HttpResponseRedirect('/accounts/articles/comments/'+str(article_id)) else: form = CommentForm() try: comments = Comment.objects.filter(article_id=article_id) return render_to_response(template, {"form":form, "comments":comments, "article_id":article_id, "full_name":request.user.username},\ RequestContext(request)) except: return render_to_response(template, {"form":form, "article_id":article_id, "full_name": request.user.username}, RequestContext(request))
def school_detail(request, school_id): latitude, longitude, has_coordinate = utils.get_coordinate_from_request( request) school = get_object_or_404(SecondarySchoolProxy, id=school_id) # queryset and pagination queryset = SchoolComment.objects.filter(school=school) paginator = Paginator(queryset, 10) # one page contains 10 items page = request.GET.get('page') try: comment_list = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. comment_list = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. comment_list = paginator.page(paginator.num_pages) comment_form = '' if request.user.is_authenticated(): if request.POST: comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.school = school comment.created_by = request.user comment.save() return HttpResponseRedirect( request.META.get('HTTP_REFERER')) comment_form = CommentForm() return render( request, 'app/school/school_detail.html', { 'school': school, 'comment_form': comment_form, 'comment_list': comment_list, 'has_coordinate': has_coordinate, 'latitude': latitude, 'longitude': longitude })
def post_details(request, post_id): post = get_object_or_404(Post, pk=post_id) # comments = post.comment.filter(active=True) comments = post.comment.all() comment_form = CommentForm() if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.save() comment_form = CommentForm() else: comment_form = CommentForm() context = { 'title': 'تفاصيل التدوينة', 'post': post, 'comments': comments, 'comment_form': comment_form, } return render(request, 'app/post_details.html', context)
def post_detail(req, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__day=day, publish__year=year, publish__month=month) comments = post.comments.filter(active=True) # comments is related name csubmit = False if req.method == 'POST': form = CommentForm(req.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() csubmit = True else: form = CommentForm() return render(req, 'post_detail.html', { 'post': post, 'form': form, 'csubmit': csubmit, 'comments': comments })