Python Forum

Full Version: Sorting getting off, when I switch page Django 1.11
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I write the pagination and and sorting. But sorting getting off, when I switch page Django 1.11...
from django import forms
class FilterForm(forms.Form):
    ordering = forms.ChoiceField(label="сортировка", required=False, choices=[
        ['name', 'Name (A-Z)'],
        ['-name', 'Name (Z-A)'],
        ['default_action_price', 'Price (Low > High)'],
        ['-default_action_price', 'Price (High > Low)'],
        ['rating', 'Rating (Highest)'],
        ['-rating', 'Rating (Lowest)'],
def index(request, slug, page_number=1):
    categories = Category.objects.all()
    subcategories = Subcategory.objects.all()
    category_item = Subcategory.objects.filter(slug=slug)
    all_products = Product.objects.all()
    page = request.GET.get('page', 1)
    paginator = Paginator(all_products, 2)
        productpages =
    except PageNotAnInteger:
        productpages =
    except EmptyPage:
        productpages =
    form = FilterForm(request.GET)
    if form.is_valid():
        if form.cleaned_data["ordering"]:
            all_products = all_products.order_by(form.cleaned_data["ordering"])
    context = {
        'categories': categories,
        'subcategories': subcategories,
        'category_item': category_item,
        'productpages': productpages,
        'all_products_count': all_products.count(),
        'start_index': productpages.start_index(),
        'end_index': productpages.end_index(),
    return render(request, 'products_list.html', context)
<div class="pull-right">
                  <form action="" method="GET">
                    <select class="form-control input-sm" name="ordering" id="id_ordering" onchange='this.form.submit()'>
                      <option value="" selected="selected">Default</option>
                      <option value="name">Name (A-Z)</option>
                      <option value="-name">Name (Z-A)</option>
                      <option value="default_action_price">Price (Low &gt; High)</option>
                      <option value="-default_action_price">Price (High &gt; Low)</option>
                      <option value="rating">Rating (Highest)</option>
                      <option value="-rating">Rating (Lowest)</option>
                      <noscript><input type="submit" value="Submit"></noscript>
<div class="row">
              <div class="col-md-4 col-sm-4 items-info">Items {{ start_index }} to {{ end_index }} of {{ all_products_count }} total</div>
              <div class="col-md-8 col-sm-8">
                {% if productpages.has_other_pages %}
                  <ul class="pagination pull-right">
                    {% if productpages.has_previous %}
                      <li><a href="?page={{ productpages.previous_page_number }}">&laquo;</a></li>
                    {% else %}
                      <li class="disabled"><span>&laquo;</span></li>
                    {% endif %}
                    {% for i in productpages.paginator.page_range %}
                      {% if productpages.number == i %}
                        <li><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                      {% else %}
                        <li><a href="?page={{ i }}">{{ i }}</a></li>
                      {% endif %}
                    {% endfor %}
                    {% if productpages.has_next %}
                      <li><a href="?page={{ productpages.next_page_number }}">&raquo;</a></li>
                    {% else %}
                      <li class="disabled"><span>&raquo;</span></li>
                    {% endif %}
                {% endif %}
url(r'^(?P<slug>[\w-]+)$', index, name="products_list"),
For example, first page have a url:
But when I switch page, I have a url: and sorting is getting off. How I get additional path from sorting ( + &ordering=name, mb?)