def index(request): if request.method == 'GET': form = CommentForm() images = Image.objects.all().order_by('-id')[:3] comments = Comment.objects.all() return render(request, 'index.html', { 'images': images, 'comments': comments, 'form': form }) else: form = CommentForm(request.POST) form.instance.username = request.user.username form.instance.photo_id = request.POST['txt'] if form.is_valid(): form.save() return redirect('/') else: return HttpResponse(form )
def film(request, pk): film = get_object_or_404(Film, id=pk) form = CommentForm(request.POST or None) if form.is_valid(): form.instance.user = request.user form.instance.film = film form.save() return redirect(film) return render(request, 'main/film.html', {'film': film, "form": form})
def add_comment_page(request, **kwargs): form = CommentForm() slug = kwargs['slug'] post =Post.objects.get(slug=slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid: form.instance.post_id = post.id form.save(commit=True) messages.success(request,'Your comment has been submitted and will be visible after the site owner approves',extra_tags='alert') return HttpResponseRedirect(reverse('content',kwargs={'slug':slug}) + '#comments')
def post(request): if request.method == 'POST': comment_form = CommentForm(request.POST) new_comment = comment_form.save(commit=False) new_comment.user = request.user new_comment.post = my_models.Post.objects.get( id=request.GET.get("post_id")) new_comment.save() return redirect(request.get_full_path()) else: article = get_object_or_404(my_models.Post, id=request.GET.get("post_id")) tags = my_models.Tag.objects.all() auth_count = User.objects.count() ix = randint(0, auth_count - 1) random_user = User.objects.all()[ix:ix + 1][0] comment_form = CommentForm() return render( request, 'main/post.html', { "article": article, 'parts': my_models.Part.objects.all(), "tags": tags, "random_user": random_user, "comment_form": comment_form, "four_popular_posts": sorted(list(my_models.Post.objects.all()), key=lambda x: x.comment.count(), reverse=True)[0:4], })
def post(self, request, *args, **kwargs): form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.page = self.page comment.save() return HttpResponseRedirect(self.request.path_info)
def post_details(request, post_list_id): try: latest_post = Post.objects.get(id=post_list_id) except Post.DoesNotExist: raise Http404("Post not found") if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form.save() return redirect('/blog/'+str(post_list_id)+'/') else: form = CommentForm() context = { 'latest_post': latest_post, 'comments': latest_post.comments.all(), 'form': form } return render(request, 'main/detail.html', context)
def index_2(request, pk): new = get_object_or_404(Posts, id=pk) comment = Comments.objects.filter(post=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form = form.save(commit=False) form.user = request.user form.post = new form.save() return redirect(index_2, pk) else: form = CommentForm() return render(request, 'main/index_2.html', { 'new': new, 'comment': comment, 'form': form })
def post(self, request, id): context = {} message = Message.objects.get(id=id) user = request.user if request.POST['type'] == 'comment': comments = Comment.objects.filter( message=message).order_by('-date_posted') context = {'comments': comments} form = CommentForm(request.POST) if form.is_valid: comment = form.save() context['comment'] = 'Youre comment has been saved' context['message'] = message else: context['comment'] = form.errors return render(request, 'message-detail.html', context) elif request.POST['type'] == 'edit': id = request.POST.get('id') if id: form = MessageForm( request.POST, request.FILES, instance=message) if form.is_valid: message = form.save() context['text'] = "Your message has been saved" context['message'] = message else: context['text'] = form.errors return render(request, 'message-detail.html', context) elif request.POST['type'] == 'favorite': user.favorites.add(message) return HttpResponse(status=204) elif request.POST['type'] == 'unfavorite': user.favorites.remove(message) return HttpResponse(status=204)
def post_comment(request): if request.POST and request.is_ajax(): json_context = {'success': False, 'form_errors': []} form = CommentForm(request.POST) comment = form.save() if form.is_valid(): json_context['success'] = 'Комментарий успешно добавлен!' json_context['commentHtml'] = render_to_string( 'comments/post_comments.html', { 'nodes': [comment], 'can_post': True }) else: json_context['form_errors'] = form.errors return HttpResponse(json.dumps(json_context), content_type="application/json") return HttpResponseForbidden()
def post_detail(request, pk): template_name = 'post-detail.html' post = get_object_or_404(Post, pk=pk) image = post.get_image images = post.images.exclude(id=image.id) comments = post.comments.filter(active=True) new_comment = None # Comment posted if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create Comment object but don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() return render(request, template_name, locals())
def RecipeDetailView(request, pk): context = {} recipe = Recipe.objects.get(pk=pk) context['recipe_v'] = recipe active = request.user.is_authenticated() context['active'] = active # Get vote_state ------------------------------------------------ if active: h_temp = "_".join([str(pk), str(request.user.username)]) vote, created = Vote.objects.get_or_create(recipe=recipe, handle=h_temp) if created: return HttpResponseRedirect('/vote_stats_func/%s' % pk) context['vote_v'] = vote else: context['vote_v'] = 2 # Get vote stats ------------------------------------------------ vote_stat, created = VoteStat.objects.get_or_create(recipe=recipe) context['vote_stat_v'] = vote_stat # Retrieve old comments ----------------------------------------- old_comments = Comment.objects.filter(recipe=recipe) context['old_comments_v'] = old_comments # Show empty form for new comments, process new comments -------- if active: context['comments_v'] = CommentForm(initial={'recipe': pk}) if request.method == 'POST': from_form = CommentForm(request.POST) if from_form.is_valid(): new_comment = from_form.save() user_name = request.user.username time = str(new_comment.time_stamp) # Because comments might get moderated, a given comment-number # might not match the total number of allowed comments # preceding it (ie, if there are 10 comments, the next comment # will be #11 [ie, 10 + 1] but if #2 is deleted by the admin, # the subsequent comment should be #12 even though there are # only 11 comments in the list. length = len(old_comments) if length == 1: number = length else: number = old_comments[length - 2].number + 1 new_comment.user_name = user_name new_comment.handle = "_".join([str(pk), user_name, time]) new_comment.number = number new_comment.email = request.user.email new_comment.save() return HttpResponseRedirect('/recipe_detail/%s/' % pk) else: context['errors'] = new_comment.errors # Send data to template ----------------------------------------- return render_to_response('recipe_detail.html', context, context_instance=RequestContext(request))