Menor
Menor

Reputation: 300

Keeping data in the form after the search

I have got a form to filter the items on my site, but when I enter the data to filter and press search, then I got the results, but the data that I put in my form disappear. Is there a way to save the data and display it in the form after the search?

views.py that handles the filtering of the data

def HomeView(request):
  item_list = Item.objects.all()
  item_list = item_list.annotate(
      current_price=Coalesce('discount_price', 'price'))

  category_list = Category.objects.all()
  query = request.GET.get('q')

  if query:
      item_list = item_list.filter(title__icontains=query)

  cat = request.GET.get('cat')
  if cat:
      item_list = item_list.filter(category__pk=cat)

  price_from = request.GET.get('price_from')
  price_to = request.GET.get('price_to')

  if price_from:
      item_list = item_list.filter(current_price__gte=price_from)

  if price_to:
      item_list = item_list.filter(current_price__lte=price_to)

  paginator = Paginator(item_list, 10)

  page = request.GET.get('page')

  try:
      items = paginator.page(page)
  except PageNotAnInteger:
      items = paginator.page(1)
  except EmptyPage:
      items = paginator.page(paginator.num_pages)

  context = {
      'items': items,
      'category': category_list
  }
  return render(request, "home.html", context)

form in the html template:

<form method="GET" action=".">
        <div class="form-group col-md-4">
            <label for="category">Category</label>
            <select id="cat" class="form-control" name="cat">
                <option value="" selected>Choose...</option>
                <option value="" href="/home">All</option>
                {% for cat in category %}
                <option value="{{ cat.pk }}">{{ cat }}</option>
                {% endfor %}
            </select>

        </div>
        <div class="input-group">
            <input class="form-control py-2 border-right-0 border" type="search" name="q" placeholder="Brand..">
            <span class="input-group-append">
                <div class="input-group-text bg-transparent">
                    <i class="fa fa-search"></i>
                </div>
            </span>
        </div>
        <div class="input-group">
            <input class="form-control py-2 border-right-0 border" type="search" name="price_from"
                placeholder="Price from">
            <span class="input-group-append">
                <div class="input-group-text bg-transparent">
                    <i class="fa fa-search"></i>
                </div>
            </span>
        </div>
        <div class="input-group">
            <input class="form-control py-2 border-right-0 border" type="search" name="price_to"
                placeholder="Price to">
            <span class="input-group-append">
                <div class="input-group-text bg-transparent">
                    <i class="fa fa-search"></i>
                </div>
            </span>
        </div>
        <button type="submit" class="btn btn-primary">Search</button>
    </form>

Upvotes: 0

Views: 36

Answers (1)

David542
David542

Reputation: 110392

A common way to do this would be to grab the GET parameter in the template itself. In this way, you can fill the existing search into your input:

<input class="form-control py-2 border-right-0 border" type="search" name="q" 
       value="{{request.GET.q}}" placeholder="Brand..">

Upvotes: 2

Related Questions