Skip to content

Commit

Permalink
Added search functionality for a product by name, by description or b…
Browse files Browse the repository at this point in the history
…y ingredients and resolves #29
  • Loading branch information
JoyZadan committed Nov 22, 2022
1 parent 836ca76 commit 4bdc786
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
24 changes: 21 additions & 3 deletions products/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.shortcuts import render, get_object_or_404
from .models import Product, Brand
from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.contrib import messages
from django.db.models import Q
from .models import Product

# Create your views here.

Expand All @@ -8,9 +10,25 @@ def all_products(request):
""" A view to show all products, including sorting and search queries """

products = Product.objects.all()
query = None

if request.GET:
if 'q' in request.GET:
query = request.GET['q']
if not query:
messages.error(
request,
"You didn't enter any search criteria!")
return redirect(reverse('products'))

queries = (Q(name__icontains=query) |
Q(description__icontains=query) |
Q(ingredients__icontains=query))
products = products.filter(queries)

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

return render(request, 'products/products.html', context)
Expand Down
4 changes: 2 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ <h2 class="logo-font text-black my-0"><strong>Shop</strong> K-Beauty</h2>
</a>
</div>
<div class="col-12 col-lg-4 my-auto py-1 py-lg-0">
<form method="GET" action="">
<form method="GET" action="{% url 'products' %}">
<div class="input-group w-100">
<input class="form-control border border-black rounded-0" type="text" name="q"
placeholder="Search for K-Beauty products">
placeholder="Search">
<div class="input-group-append">
<button class="form-control btn btn-black border border-black rounded-0" type="submit">
<span class="icon">
Expand Down
4 changes: 2 additions & 2 deletions templates/includes/mobile_top_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ <h2 class="logo-font text-black my-0"><strong>Shop</strong> K-Beauty</h2>
</div>
</a>
<div class="dropdown-menu border-0 w-100 p-3 rounded-0 my-0" aria-labelledby="mobile-search">
<form class="form" method="GET" action="">
<form class="form" method="GET" action="{% url 'products' %}">
<div class="input-group w-100">
<input class="form-control border border-black rounded-0" type="text" name="q" placeholder="Search for K-Beauty products">
<input class="form-control border border-black rounded-0" type="text" name="q" placeholder="Search">
<div class="input-group-append">
<button class="form-control form-control btn btn-black border border-black rounded-0" type="submit">
<span class="icon">
Expand Down

0 comments on commit 4bdc786

Please sign in to comment.