from django.shortcuts import render, get_object_or_404, redirect
from django.db.models import Q

from .models import Product, Category, ProductReview

# Create your views here.

def search(request):
    query = request.GET.get('query')
    products = Product.objects.filter(Q(title__icontains=query) | Q(description__icontains=query))

    context = {
        'query': query,
        'products': products
    }

    return render(request, 'search.html', context)

def product_detail(request, category_slug, slug):
    product = get_object_or_404(Product, slug=slug) # if can't get a product where slug equals slug then get a 404 error

    # Add review

    if request.method == 'POST' and request.user.is_authenticated:
        stars = request.POST.get('stars', 3)
        content = request.POST.get('content', '')

        review = ProductReview.objects.create(product=product, user=request.user, stars=stars, content=content)

        return redirect('product_detail', category_slug=category_slug, slug=slug)

    context = {
        'product': product
    }

    return render(request, 'product_detail.html', context)

def category_detail(request, slug):
    category = get_object_or_404(Category, slug=slug)
    products = category.products.all()
    context = {
        'category': category,
        'products': products
    }

    return render(request, 'category_detail.html', context)
8e239316be683cd380ef3b511ee0f0e02f11cfcd
<mask token>


def search(request):
    query = request.GET.get('query')
    products = Product.objects.filter(Q(title__icontains=query) | Q(
        description__icontains=query))
    context = {'query': query, 'products': products}
    return render(request, 'search.html', context)


<mask token>

<mask token>


def search(request):
    query = request.GET.get('query')
    products = Product.objects.filter(Q(title__icontains=query) | Q(
        description__icontains=query))
    context = {'query': query, 'products': products}
    return render(request, 'search.html', context)


def product_detail(request, category_slug, slug):
    product = get_object_or_404(Product, slug=slug)
    if request.method == 'POST' and request.user.is_authenticated:
        stars = request.POST.get('stars', 3)
        content = request.POST.get('content', '')
        review = ProductReview.objects.create(product=product, user=request
            .user, stars=stars, content=content)
        return redirect('product_detail', category_slug=category_slug, slug
            =slug)
    context = {'product': product}
    return render(request, 'product_detail.html', context)


<mask token>

<mask token>


def search(request):
    query = request.GET.get('query')
    products = Product.objects.filter(Q(title__icontains=query) | Q(
        description__icontains=query))
    context = {'query': query, 'products': products}
    return render(request, 'search.html', context)


def product_detail(request, category_slug, slug):
    product = get_object_or_404(Product, slug=slug)
    if request.method == 'POST' and request.user.is_authenticated:
        stars = request.POST.get('stars', 3)
        content = request.POST.get('content', '')
        review = ProductReview.objects.create(product=product, user=request
            .user, stars=stars, content=content)
        return redirect('product_detail', category_slug=category_slug, slug
            =slug)
    context = {'product': product}
    return render(request, 'product_detail.html', context)


def category_detail(request, slug):
    category = get_object_or_404(Category, slug=slug)
    products = category.products.all()
    context = {'category': category, 'products': products}
    return render(request, 'category_detail.html', context)

from django.shortcuts import render, get_object_or_404, redirect
from django.db.models import Q
from .models import Product, Category, ProductReview


def search(request):
    query = request.GET.get('query')
    products = Product.objects.filter(Q(title__icontains=query) | Q(
        description__icontains=query))
    context = {'query': query, 'products': products}
    return render(request, 'search.html', context)


def product_detail(request, category_slug, slug):
    product = get_object_or_404(Product, slug=slug)
    if request.method == 'POST' and request.user.is_authenticated:
        stars = request.POST.get('stars', 3)
        content = request.POST.get('content', '')
        review = ProductReview.objects.create(product=product, user=request
            .user, stars=stars, content=content)
        return redirect('product_detail', category_slug=category_slug, slug
            =slug)
    context = {'product': product}
    return render(request, 'product_detail.html', context)


def category_detail(request, slug):
    category = get_object_or_404(Category, slug=slug)
    products = category.products.all()
    context = {'category': category, 'products': products}
    return render(request, 'category_detail.html', context)

from django.shortcuts import render, get_object_or_404, redirect
from django.db.models import Q

from .models import Product, Category, ProductReview

# Create your views here.

def search(request):
    query = request.GET.get('query')
    products = Product.objects.filter(Q(title__icontains=query) | Q(description__icontains=query))

    context = {
        'query': query,
        'products': products
    }

    return render(request, 'search.html', context)

def product_detail(request, category_slug, slug):
    product = get_object_or_404(Product, slug=slug) # if can't get a product where slug equals slug then get a 404 error

    # Add review

    if request.method == 'POST' and request.user.is_authenticated:
        stars = request.POST.get('stars', 3)
        content = request.POST.get('content', '')

        review = ProductReview.objects.create(product=product, user=request.user, stars=stars, content=content)

        return redirect('product_detail', category_slug=category_slug, slug=slug)

    context = {
        'product': product
    }

    return render(request, 'product_detail.html', context)

def category_detail(request, slug):
    category = get_object_or_404(Category, slug=slug)
    products = category.products.all()
    context = {
        'category': category,
        'products': products
    }

    return render(request, 'category_detail.html', context)