Advertisement
rlc4

Untitled

May 26th, 2024
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import datetime
  2. from django.shortcuts import render, get_object_or_404, get_list_or_404
  3. from blog.models import Post, Category, Location
  4. from django.db.models import Q
  5.  
  6.  
  7. def index(request):
  8.     template_name = 'blog/index.html'
  9.     current_time = datetime.datetime.now()
  10.     post_list = get_list_or_404(
  11.         Post,
  12.         is_published=True,
  13.         pub_date__lt=current_time,
  14.         category__is_published=True
  15.     )[:5]
  16.  
  17.     context = {
  18.         'post_list': post_list
  19.     }
  20.  
  21.     return render(request, template_name, context)
  22.  
  23.  
  24. def category_posts(request, category_slug):
  25.     template_name = 'blog/category.html'
  26.     current_time = datetime.datetime.now()
  27.     category = get_object_or_404(
  28.         Category,
  29.         slug=category_slug,
  30.         is_published=True
  31.     )
  32.  
  33.     post_list = get_list_or_404(
  34.         Post,
  35.         is_published=True,
  36.         category=category,
  37.         pub_date__lte=current_time,
  38.         category__is_published=True
  39.     )
  40.  
  41.     context = {
  42.         'category': category,
  43.         'post_list': post_list
  44.     }
  45.  
  46.     return render(request, template_name, context)
  47.  
  48.  
  49. def post_detail(request, post_id):
  50.     template_name = 'blog/detail.html'
  51.     current_time = datetime.datetime.now()
  52.  
  53.     post = get_object_or_404(
  54.         Post,
  55.         pk=post_id,
  56.         pub_date__lte=current_time,
  57.         is_published=True,
  58.         category__is_published=True
  59.     )
  60.  
  61.     context = {
  62.         'post': post
  63.     }
  64.  
  65.     return render(request, template_name, context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement