Python Forum
Sorting getting off, when I switch page Django 1.11
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting getting off, when I switch page Django 1.11
#1
Hi. I write the pagination and and sorting. But sorting getting off, when I switch page Django 1.11...

forms.py
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)'],
    ])
views.py
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)
 
    try:
        productpages = paginator.page(page)
    except PageNotAnInteger:
        productpages = paginator.page(1)
    except EmptyPage:
        productpages = paginator.page(paginator.num_pages)
 
    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)
sorting
<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>
                    </select>
                      <noscript><input type="submit" value="Submit"></noscript>
                    </form>
                </div>
pagination
<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 %}
                  </ul>
                {% endif %}
 
            </div>
          </div>
urls.py
url(r'^(?P<slug>[\w-]+)$', index, name="products_list"),
For example, first page have a url: https://127.0.0.1:8000/products/ice-cream?ordering=name.
But when I switch page, I have a url: https://127.0.0.1:8000/products/ice-cream?page=2 and sorting is getting off. How I get additional path from sorting (https://127.0.0.1:8000/products/ice-cream?page=2 + &ordering=name, mb?)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Django] css file is not applying to the page SheeppOSU 1 551 May-09-2020, 06:54 AM
Last Post: menator01
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 866 Mar-19-2020, 06:13 PM
Last Post: apollo
  Unable to switch out of nested frames into main page abi17124 0 630 Jul-17-2019, 06:06 PM
Last Post: abi17124
  Django Two blocks of dynamic content on one page iFunKtion 5 1,371 Jul-04-2019, 02:31 AM
Last Post: noisefloor
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 986 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django-cms link to a page Alkatron 4 2,456 Apr-06-2018, 10:58 AM
Last Post: Alkatron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020