def post_view(request, username, post_id): post = get_object_or_404(Post, id=post_id, author__username=username) all_comments = post.comments.all() comment_form = CommentForm(request.POST or None) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.author = request.user new_comment.post = post new_comment.save() comment_form = CommentForm() # проверим, подписан ли пользователь на автора поста if request.user.is_authenticated: count = Follow.objects.filter(user=request.user, author__username=username).count() follows = True if count > 0 else False else: follows = False return render( request, 'post.html', { 'post': post, 'author': post.author, 'form': comment_form, 'following': follows, 'comments': all_comments })
def add_comment(request, username, post_id): user_req = get_object_or_404(User, username=username) post = get_object_or_404(Post, pk=post_id) comments = post.comments.all() if request.method == "POST": form = CommentForm(data=request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = post comment.save() return redirect("post", username, post_id) return render(request, "post.html", { "user_req": user_req, "post": post, "form": form, "comments": comments }) form = CommentForm() return render(request, "post.html", { "user_req": user_req, "post": post, "form": form, "comments": comments })
def p_read(request, post_id): post = get_object_or_404(Post, pk=post_id) session = request.session['user'] if request.method == 'POST': # POST 방식으로 호출 될 때 user_id = request.session.get('user') user = User.objects.get(username=user_id) comment = Comment(post=post, author=user_id) comment_form = CommentForm(request.POST, instance=comment) if comment_form.is_valid(): comment_form.save() return redirect(f'/posts/{post_id}/read') else: # GET 방식으로 호출 될 때 comment_form = CommentForm() context = { 'post': post, 'comment_form': comment_form, 'session': session } return render(request, 'read.html', context)
def post(self, request, slug): post = self.get_object() if self.request.method == 'POST': form = CommentForm(self.request.POST) if form.is_valid(): form.instance.post_id = post.id form.save() else: form = CommentForm() return HttpResponseRedirect(reverse('posts:post-detail', kwargs={'slug': post.slug}))
def profile(request, username, profile=None, user_profile=None): if profile is None: profile = Profile.objects.get(user=User.objects.get(username=username)) posts = profile.posts.order_by('-post_date').all() paginador = Paginator(posts, 10) page = request.GET.get('page') posts = paginador.get_page(page) if request.user.is_authenticated: if request.user.username == username: return render( request, 'profile.html', { 'profile': profile, 'comment_form': CommentForm(), 'share_form': SharePostForm(), 'user_profile': Profile.objects.get(user=request.user), 'posts': posts }) friendship = 0 invitation = None try: invitation = Invitation.objects.get( Q(sender__user__username=request.user.username, receiver__user__username=username) | Q(sender__user__username=username, receiver__user__username=request.user.username)) except Invitation.DoesNotExist: pass if invitation is not None: if invitation.sender.user == request.user: friendship = 1 if invitation.sender.user == profile.user: friendship = 2 elif profile.contacts.filter(user=request.user).exists(): friendship = 3 return render( request, 'profile.html', { 'user_profile': Profile.objects.get(user=request.user), 'comment_form': CommentForm(), 'share_form': SharePostForm(), 'profile': profile, 'friendship': friendship, 'invitation': invitation, 'posts': posts }) return render(request, 'profile.html', { 'profile': profile, 'posts': posts })
def comment(request, slug): post = get_object_or_404(Post, slug=slug) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('posts:detail', slug=post.slug) else: form = CommentForm() return render(request, 'posts/comment.html', {'form': form})
def add_comment_to_post(request, title_slug): post = get_object_or_404(Post, slug=title_slug) 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', title_slug=post.slug) else: form = CommentForm() return render(request, 'posts/comment_form.html', {'form': form})
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() else: form = CommentForm() return render(request, 'posts/comment_form.html', {'form': form})
def newcomment(request,pk): if request.method=='POST': form=CommentForm(request.POST) if form.is_valid: c=form.save(commit=False) c.c_author=request.user c.post=Post.objects.get(pk=pk) form.save() return redirect('postdetail',pk=pk) else: form=CommentForm() return render(request,'createcomment.html',{'form':form})
def add_comment_to_post(request, post_id): post = get_object_or_404(Post, pk=post_id) if request.method == "POST": comment = Comment(post=post) comment_form = CommentForm(request.POST, instance=comment) if comment_form.is_valid(): comment_form.save() path = '/posts/{}/'.format(post_id) return redirect(path) else: comment_form = CommentForm() return render(request, 'add_comment_to_post.html', {'comment_form':comment_form})
def add_comment_to_post(request, id): post = get_object_or_404(Post, pk=id) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.author = request.user comment.save() return redirect('posts:detail', id=post.id) else: form = CommentForm() return render(request, 'my_blog/add_comment_to_post.html', {'form': form})
def create_comment(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.author = request.user comment.save() return HttpResponseRedirect( reverse('posts-details-page', args=[post.pk])) else: form = CommentForm() return render(request, 'create_comment.html', {'form': form})
def post_detail_view(request, slug): post = Post.objects.get(slug=slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form.instance.post = post form.instance.author = request.user.userprofile form.save(commit=True) reverse_lazy('posts:detail', kwargs={'slug': slug}) form = CommentForm() return render(request, 'posts/detail.html', {'post': post, 'form': form})
def comment_create(request, pk): if request.method == 'POST': post = get_object_or_404(Post, pk=pk) # content = request.get('content') # # if not content: # return HttpResponse('댓글내용을 입력하세요.', status=400) # # Comment.objects.create( # author=request.user, # post=post, # content=content, # ) # return redirect() comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.author = request.user comment.save() messages.success(request, '댓글이 등록되었습니다') else: error_msg = '댓글 등록에 실패했습니다\n{}'.format( '\n'.join( [f'- {error}' for key, value in comment_form.errors.items() for error in value])) messages.error(request, error_msg) return redirect('post:post-detail', pk=pk)
def search(request): q1 = request.GET.get('question') Q = q1.split(' ') if Q == ['']: return redirect('mywall') i = 0 results = Post.objects.none() while i < len(Q): results = results | Post.objects.filter(text__icontains=Q[i])[:10] i += 1 profile = Profile.objects.get(user=request.user) if profile.has_unread_notif(): notif = True else: notif = False subscrib_onme = Subscrib.objects.filter(to=request.user) my_subscribs = Subscrib.objects.filter(who=request.user) form_com = CommentForm(prefix='comment') context = { 'results': results, 'profile': profile, 'notif': notif, 'subscrib_onme': subscrib_onme, 'my_subscribs': my_subscribs, 'form_com': form_com } return render(request, 'root/search_results.html', context)
def get_context_data(self, *, object_list=None, **kwargs): ctx = super().get_context_data(**kwargs) ctx['comment_form'] = ctx.get('comment_form', CommentForm()) # obj = super().get_object() self.object.set_can_edit(self.request) # ctx['can_edit'] = self.object.can_edit(self.object) return ctx
def view_account(request, slug): profile = get_object_or_404(Profile, slug=slug) user = profile.user current_user = request.user # Used when loading the view without any button clicks # (to determine the color and text of the following button). is_following = current_user.profile.following.\ filter(pk=profile.id).exists() is_followed = current_user.profile.followers.\ filter(pk=profile.id).exists() posts_list = user.posts.all() posts = get_paginated_posts(request, posts_list) args = { 'user': user, 'profile': profile, 'current_user': current_user, 'posts': posts, 'is_following': is_following, 'is_followed': is_followed, 'comment_form': CommentForm() } if user == current_user and request.method == 'GET': post_form = PostForm() args.update({'post_form': PostForm()}) return render(request, 'accounts/view_account.html', args)
def detail_post(request, pk): """Detail Post.""" post = Post.objects.get(pk=pk) comment_form = CommentForm(request.POST or None) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() comment_form = CommentForm() return render(request, 'posts/detail_post.html', { 'post': post, 'form': comment_form })
def setUpClass(cls): super().setUpClass() cls.form = PostForm() cls.form_comment = CommentForm() User = get_user_model() cls.user2 = User.objects.create(username="******") cls.user = User.objects.create(username="******") cls.authorized_client = Client() cls.authorized_client.force_login(cls.user) cls.group = Group.objects.create( title="Заголовок", description="Описание", slug="Slug_test", ) cls.post = Post.objects.create(author=PostFormTests.user, text="Какой-нибудь текст", group=cls.group) cls.comment = Comment.objects.create( post=cls.post, author=PostFormTests.user, text="Текст тестового комментария", )
def post_preprocessor(data, **kwargs): form = CommentForm(data=data) if form.validate(): return form.data else: raise ProcessingException(description='Invalid form submission.', code=400)
def post_list(request): posts = Post.objects.all() comment_form = CommentForm() context = { 'posts': posts, 'comment_form': comment_form, } return render(request, 'posts/post_list.html', context)
def get_context_data(self, **kwargs): context = super(PostView, self).get_context_data(**kwargs) context['form'] = CommentForm() post = Post.objects.get(slug=self.kwargs.get('slug')) context['comments'] = Comment.objects.filter( post=post).order_by('-created_at') context['title'] = post.title return context
def post_detail(request, post_pk): post = Post.objects.get(pk=post_pk) context = { 'post': post, 'comment_form': CommentForm(), } return render(request, 'posts/post_detail.html', context)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) comments = Comment.objects.filter( post=self.get_object()).order_by('-created') context['comments'] = comments context['CommentForm'] = CommentForm(instance=self.request.user) return context
def add_comment(request, username, post_id): post = get_object_or_404(Post, id=post_id, author__username=username) form = CommentForm(request.POST or None) if form.is_valid(): form.instance.author = request.user form.instance.post = post form.save() return redirect('post', username=username, post_id=post_id)
def new_comment(request, post_id, comment_id=None): current_user = request.user if not current_user.is_authenticated: return HttpResponseNotAllowed(['GET', 'POST']) if request.method == 'POST': comment_form = CommentForm(request.POST, request.FILES) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.author = current_user comment.post = Post.objects.filter(id=post_id)[0] comment.parent = None if comment_id is None else Comment.objects.filter( id=comment_id)[0] comment.save() return HttpResponseRedirect('/') else: comment_form = CommentForm() return render(request, "comments/new_comment.html", {'form': comment_form})
def post_detail(request, pk): post = get_object_or_404(Post, pk=pk) comment_form = CommentForm() context = { 'post': post, 'comment_form': comment_form, } return render(request, 'posts/post_detail.html', context)
def post(self, request, pk, *args, **kwargs): user = request.user profile = Profile.objects.get(user=user) group = Group.objects.get(pk=pk) # Custom profanity words custom_badwords = CustomProfanity.objects.values_list('bad_word', flat=True) profanity.load_censor_words(custom_badwords) if 'submit_post_form' in request.POST: post_form = PostForm(request.POST, request.FILES) comment_form = None valid = post_form.is_valid() if profanity.contains_profanity( post_form.cleaned_data.get('content')): custom_profanity_error = 'Please remove any profanity/swear words. (Added by an admin. Contact an admin if you believe this is wrong.)' valid = False post_form.errors['content'] = custom_profanity_error if valid: post_instance = post_form.save(commit=False) post_instance.author = profile post_instance.group = group post_instance.save() return redirect('groups:view-group', pk=pk) elif 'submit_comment_form' in request.POST: comment_form = CommentForm(request.POST) post_form = None valid = comment_form.is_valid() if profanity.contains_profanity( comment_form.cleaned_data.get('body')): custom_profanity_error = 'Please remove any profanity/swear words. (Added by an admin. Contact an admin if you believe this is wrong.)' valid = False comment_form.errors['body'] = custom_profanity_error if valid: post_id = request.POST.get("post_id") comment_instance = comment_form.save(commit=False) comment_instance.user = profile comment_instance.post = Post.objects.get(id=post_id) comment_instance.save() return redirect(request.headers.get('Referer')) group_posts = Post.objects.filter(group=group) context = { 'profile': profile, 'group': group, 'posts': group_posts, 'post_form': post_form, 'comment_form': comment_form } return render(request, 'groups/view.html', context)
def create_comments(request, **kwargs): # ipdb.set_trace() post = get_object_or_404(Post, slug=kwargs['slug']) node_set = post.comment_set.all() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): user_obj = request.user form.instance.fk_user = user_obj try: post_obj = Post.objects.get(slug=kwargs['slug']) form.instance.fk_post = post_obj # if reply if kwargs.has_key('p_id'): form.instance.parent_cmt = Comment.objects.get(id=kwargs['p_id']) form.save() form.save() node_set = post_obj.comment_set.all() except ObjectDoesNotExist: raise Http404 return render(request, 'posts/post_detail.html', {'post': post_obj, 'node_set': node_set,}) else: form = CommentForm() if kwargs.has_key('p_id'): form.instance.parent_cmt = Comment.objects.get(id=kwargs['p_id']) form_type = 'reply_' else: form_type = 'comment_' else: form = CommentForm() if kwargs.has_key('p_id'): form.instance.parent_cmt = Comment.objects.get(id=kwargs['p_id']) form_type = 'reply_' else: form_type = 'comment_' return render(request, 'posts/comment_form.html', {'form': form, 'post': post, 'node_set': node_set, 'form_type': form_type})
def CommentView(request, id): post = get_object_or_404(Post, pk=id) form = CommentForm(request.POST) if form.is_valid(): my_new_todo = Post(commentext=request.POST['text']) my_new_todo.save() return redirect('post_detail', pk=post.pk)