from django.shortcuts import render
from blog.models import Post, Category


def home(request):
    """View function for home page"""
    published_posts = Post.objects.all().filter(published=True)

    context = {
        'published_posts':published_posts,
    }

    return render(request=request, template_name='pages/home.html', context=context)


def list_of_categories(request):
    """View function for Category"""
    all_categories = Category.objects.all()

    context = {
        'all_categories' : all_categories
    }

    return  render(request=request, template_name="pages/category.html", context=context)


def posts_by_category(request, category_slug):
    """View that lists the posts which belong to a category."""
    list_of_posts = Post.objects.all().filter(published=True).filter(category__category_slug=category_slug)

    context = {
        "list_of_posts":list_of_posts,

    }

    return render(request=request, template_name="pages/posts_by_category.html", context=context)


def post_detail(request, blog_slug):
    """ View function for Blog post detail"""
    blog_post = Post.objects.get(blog_slug=blog_slug)

    context = {
        'blog_post':blog_post
    }

    return render(request=request, context=context, template_name="pages/blog_detail.html")


from django.shortcuts import render
from blog.models import Post, Category


def home(request):
    """View function for home page"""
    published_posts = Post.objects.all().filter(published=True)

    context = {
        'published_posts':published_posts,
    }

    return render(request=request, template_name='pages/home.html', context=context)


def list_of_categories(request):
    """View function for Category"""
    all_categories = Category.objects.all()

    context = {
        'all_categories' : all_categories
    }

    return  render(request=request, template_name="pages/category.html", context=context)


def posts_by_category(request, category_slug):
    """View that lists the posts which belong to a category."""
    list_of_posts = Post.objects.all().filter(published=True).filter(category__category_slug=category_slug)

    context = {
        "list_of_posts":list_of_posts,

    }

    return render(request=request, template_name="pages/posts_by_category.html", context=context)


def post_detail(request, blog_slug):
    """ View function for Blog post detail"""
    blog_post = Post.objects.get(blog_slug=blog_slug)

    context = {
        'blog_post':blog_post
    }

    return render(request=request, context=context, template_name="pages/blog_detail.html")


View function for home page View function for Category View that lists the posts which belong to a category. View function for Blog post detail