alcoder
alcoder

Reputation: 457

Wrong url in pagination. Django

I want to search by two fields (q1 and q2): 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 "search" I go to http://127.0.0.1:8001/search/?q=mos&q2=RU (it's OK)

Then I click "next". I go to

http://127.0.0.1:8001/search/?city=2&q=mos&q2=%20RU (ERROR: "Page not found)

but I want

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

How can I fix it? Why do I have "%20" ???

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 }}&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>
</div>

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

urls.py

urlpatterns = [
    path('search/', SearchResultsView.as_view(), name='search_results'),
    path('', HomePageView.as_view(), name='home'),
    path('city/<int:pk>/', views.city_detail, name='city_detail'),
]

views.py

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')
        # added param
        context['query2'] = self.request.GET.get('q2')
        #print("BBBBBBBBBBBBBBBBb {}".format(context['query2']))
        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

Upvotes: 1

Views: 213

Answers (1)

PRMoureu
PRMoureu

Reputation: 13317

%20 is the url code for a space. You need to remove the extra spaces in :

<a href="/search?city={{ page_obj.previous_page_number }}&q={{ query }}&q2={{query2}}">previous</a>
#                                                                          ^                                                                 

and

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

Upvotes: 3

Related Questions