def post_detail(request, post_id): post = get_object_or_404(Post, pk=post_id) post_same_category = Post.objects.filter(published=True, category=post.category) \ .exclude(pk=post.id) comments = post.comments.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment_form.save() else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'post_same_category': post_same_category, 'comments': comments, 'comment_form': comment_form, } return render(request, 'blog/post_detail.html', context)
def post_detail(request, post_id, message=''): post = get_object_or_404(Post, pk=post_id) posts_same_category = Post.objects.filter( published=True, category=post.category).exclude(pk=post_id) comments = post.comments.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') comment_form = CreateCommentForm() if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() args = [post.pk, 'Your comment has been posted!'] return HttpResponseRedirect( reverse('post-detail-message', args=args) + '#comments') else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'posts_same_category': posts_same_category, 'comments': comments, 'comment_form': comment_form, 'message': message, } return render(request, 'blog/post_detail.html', context)
def post_detail(request, post_id, message=''): post = get_object_or_404(Post, pk=post_id) related = model_helpers.get_related_posts(post) comments = model_helpers.get_comments(post) if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() args = [post.pk, 'Your comment has been submitted.'] return HttpResponseRedirect(reverse('post-detail-message', args=args) + '#comments') else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'related': related, 'comments': comments, 'comment_form': comment_form, 'message': message, } return render(request, 'blog/post_detail.html', context)
def post_list(request, category_name=model_helpers.post_category_all.slug()): categories = model_helpers.get_categories() category, posts = model_helpers.get_category_and_posts(category_name) context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'categories': categories, 'category': category, 'posts': posts, } return render(request, 'blog/post_list.html', context)
def post_list(request, category_name=model_helpers.post_category_all.slug()): # model_helpers return a tuple (category, posts) category, posts = model_helpers.get_category_and_posts( category_name) # allows loading all posts categories = model_helpers.get_categories() # get_categories et get_category_name are separated from views to reusing in others spots context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'category': category, 'posts': posts, 'categories': categories, } return render(request, 'blog/posts_list.html', context)
def post_detail(request, post_id): post = get_object_or_404(Post, pk=post_id) posts_same_category = Post.objects.filter( published=True, category=post.category).exclude(pk=post_id) # Use the foreign key to get the related comment comments = post.comments_FK.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') context = { 'navitation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'posts_same_category': posts_same_category, 'comments': comments } return render(request, 'blog/post_detail.html', context)
def post_detail(request, post_id, message=''): post = get_object_or_404(Post, pk=post_id) # get_object_or_404 allows to get a post # variable that contents list of post in same category posts_same_category = Post.objects.filter(published=True, category=post.category) \ .exclude(pk=post_id) # short comment by status and order by created date comments = post.comments.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') # treatment of form comment # tests if the method HTTP used is a post request so form was sent if request.method == 'POST': comment_form = CreateCommentForm(request.POST) # request.POST a parameter give to constructor that gather data content in form # checks form validity if comment_form.is_valid(): comment = comment_form.save(commit=False) # associate comment to a post by FK comment.post = post # save in BDD comment.save() args = [post.pk, 'Thanks, your comment has been posted'] # display message to user to confirm submit of form content # post-detail-message is url of redirection if a post wa submitted return HttpResponseRedirect( reverse('post-detail-message', args=args) + '#comments') # to place browser near the id that represents comment that posted else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'posts_same_category': posts_same_category, 'comments': comments, 'comment_form': comment_form, 'message': message, } return render(request, 'blog/post_detail.html', context)
def about(request): context = { 'navigation_items': navigation.navigation_items(navigation.NAV_ABOUT), } return render(request, 'blog/about.html', context)