alcoder
alcoder

Reputation: 457

Implement search by two forms django

I want to implement search by two parameters form: home.html

<form action="{% url 'search_results' %}" method="get">
    <input name="q" type="text" placeholder="Search...">
    <select name="q2" class="form-control" id="exampleFormControlSelect1">
        <option>All locations</option>
        <option>RU</option>
        <option>Ukraine</option>
        <option>USA</option>
    </select>
    <button> Search </button>
</form>

When I click to "search" it's OK (I go to http://127.0.0.1:8001/search/?q=mos&q2=RU) But when I click "next", I receive

http://127.0.0.1:8001/search/?city=2&q=mos&q2=

but I need

http://127.0.0.1:8001/search/?city=2&q=mos&q2=RU

How can I fix this?

<a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}&q2= {{query}}">next</a>

Full code: search_results.html

<h1>Search Results</h1>

<ul>
  {% for city in object_list %}
    <li>
      {{ city.name }}, {{ city.state }}
    </li>
  {% endfor %}
</ul>

<div class="pagination">
    <span class="page-links">
        {% if page_obj.has_previous %}
            <a href="/search?city={{ page_obj.previous_page_number }}&q={{ query }}">previous</a>

        {% endif %}
            <span class="page-current">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
            </span>
        {% if page_obj.has_next %}
            <a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}&q2= {{query}}">next</a>
        {% endif %}
    </span>
</div>

views.py

from django.shortcuts import render
from django.views.generic import TemplateView, ListView
from .models import City
from django.db.models import Q
from django.shortcuts import render, get_object_or_404


class HomePageView(ListView):
    model = City
    template_name = 'cities/home.html'
    paginate_by = 3
    page_kwarg = 'city'

def city_detail(request, pk):
    city = get_object_or_404(City, pk=pk)
    return render(request, 'cities/city_detail.html', {'city': city})


class SearchResultsView(ListView):
    model = City
    template_name = 'cities/search_results.html'
    paginate_by = 3
    page_kwarg = 'city'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['query'] = self.request.GET.get('q')
        return context

    def get_queryset(self): # new
        query = self.request.GET.get('q')
        query2 = self.request.GET.get('q2')
        print("AAAAAAAAAAAAAAAAAAAAA", query2, type(query2))
        object_list = City.objects.filter(
            (Q(name__icontains=query) | Q(state__icontains=query)) & Q(category=query2)
        )
        return object_list

models.py

from django.db import models

class City(models.Model):
    name = models.CharField(max_length=255)
    state = models.CharField(max_length=255)
    COUNTRY = (
        ('RU', 'Russia'),
        ('UKR', 'Ukraine'),
        ('US', 'USA'),
    )
    category = models.CharField(max_length=100, choices=COUNTRY, default='RU')

    class Meta:
      verbose_name_plural = "cities"

    def __str__(self):
        return self.name

Upvotes: 0

Views: 48

Answers (1)

Rohan
Rohan

Reputation: 53316

You need to update get_context_data to pass q2 as well to template evaluator. And use that in building url in template.

Something like this

def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['query'] = self.request.GET.get('q')
        #added param
        context['query2'] = self.request.GET.get('q2')
        return context

In template use query2 as

<span class="page-links">
        {% if page_obj.has_previous %}
            <a href="/search?city={{ page_obj.previous_page_number }}&q={{ query }}&q2= {{query2}}">previous</a>

        {% endif %}
            <span class="page-current">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
            </span>
        {% if page_obj.has_next %}
            <a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}&q2= {{query2}}">next</a>
        {% endif %}
</span>

Note: you are not using q2 in link for previous, add that too.

Upvotes: 1

Related Questions