def post_details(request, pk): post = UserPost.objects.get(pk=pk) title = post.text[:15] + "[...]" comments = UserPostComment.objects. \ filter(post=post).order_by('-date_added') context = { 'post': post, 'title': title, 'comments': comments, } if request.method == 'GET': form = UserPostCommentForm() context['form'] = form return render(request, 'post_details.html', context) elif request.method == 'POST': form = UserPostCommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] author = form.cleaned_data['author'] user_comment = UserPostComment(text=text, author=author, post=post) user_comment.save() return redirect('post_details', pk=pk) else: context['form'] = form return render(request, 'post_details.html', context)
def post_details(request, pk): if (request.method == 'GET'): form = UserPostCommentForm() post = UserPost.objects.get(pk=pk) comments = UserPostComment.objects.filter(post=post) \ .order_by('-date_added') context = {'post': post, 'comments': comments, 'form': form} return render(request, 'post_details.html', context) elif request.method == 'POST': form = UserPostCommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] comment = UserPostComment(text=text) comment.post_id = pk comment.save() return redirect('/post/{}/'.format(pk))
def post_details(request, pk): if request.method == 'POST': form = UserPostCommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] comment = UserPostComment(text=text) comment.post_id = pk comment.save() post = UserPost.objects.get(pk=pk) comments = post.userpostcomment_set.all().order_by('-date_added') form = UserPostCommentForm() context = {'post': post, 'comments': comments, 'form': form} return render(request, 'post_details.html', context)
def post_details(request, pk): post = UserPost.objects.get(pk=pk) if request.method == 'GET': form = UserPostCommentForm() context = { 'post': post, 'form': form, } return render(request, 'post_details.html', context) elif request.method == 'POST': form = UserPostCommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] comment = UserPostComment(text=text, post=post, author=request.user) comment.save() return redirect('post_details', pk=pk)
def post_details(request, pk): post = UserPost.objects.get(pk=pk) if request.method == 'GET': comments = UserPostComment.objects.filter( post_id=post).order_by('-date_added') form = UserPostCommentForm() context = { 'post': post, 'form': form, 'comments': comments, } return render(request, 'post_details.html', context) elif request.method == 'POST': form = UserPostCommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] user_post_comment = UserPostComment(text=text, post=post) user_post_comment.save() return redirect('post_details', pk=pk)