Advertisement
ksieradzinski

Untitled

Jun 17th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. from django.shortcuts import render, get_object_or_404
  2.  
  3. from blog.models import Post, Category, Comment
  4.  
  5.  
  6. def posts_list(request):
  7. return render(
  8. request,
  9. 'blog/posts_list.html',
  10. {
  11. 'posts': Post.objects.filter(published=True).order_by('-published_at'),
  12. })
  13.  
  14.  
  15. def posts_from_category(request, slug):
  16. category = get_object_or_404(Category, slug=slug)
  17. posts = Post.objects.filter(category=category, published=True).order_by('-published_at')
  18.  
  19. return render(
  20. request,
  21. 'blog/posts_from_category.html',
  22. {
  23. 'posts': posts,
  24. 'category': category
  25. })
  26.  
  27.  
  28. def post_show(request, slug):
  29. post = get_object_or_404(Post, slug=slug, published=True)
  30. comments = Comment.objects.filter(post=post)
  31.  
  32. return render(
  33. request,
  34. 'blog/post_show.html',
  35. {
  36. 'post': post,
  37. 'comments': comments
  38. }
  39. )
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement