Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from django.shortcuts import render, get_object_or_404, get_list_or_404
- from blog.models import Post, Category, Location
- from django.db.models import Q
- def index(request):
- template_name = 'blog/index.html'
- current_time = datetime.datetime.now()
- post_list = get_list_or_404(
- Post,
- is_published=True,
- pub_date__lt=current_time,
- category__is_published=True
- )[:5]
- context = {
- 'post_list': post_list
- }
- return render(request, template_name, context)
- def category_posts(request, category_slug):
- template_name = 'blog/category.html'
- current_time = datetime.datetime.now()
- category = get_object_or_404(
- Category,
- slug=category_slug,
- is_published=True
- )
- post_list = get_list_or_404(
- Post,
- is_published=True,
- category=category,
- pub_date__lte=current_time,
- category__is_published=True
- )
- context = {
- 'category': category,
- 'post_list': post_list
- }
- return render(request, template_name, context)
- def post_detail(request, post_id):
- template_name = 'blog/detail.html'
- current_time = datetime.datetime.now()
- post = get_object_or_404(
- Post,
- pk=post_id,
- pub_date__lte=current_time,
- is_published=True,
- category__is_published=True
- )
- context = {
- 'post': post
- }
- return render(request, template_name, context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement