Ejemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        sortForm = SortForm(request.GET or None)
        value = request.GET.get('query', '')
        sort_by = request.GET.get('sort_by', '')
        # store search query in the database
        if len(value) > 2:
            term = SearchTerm()
            term.query = value
            term.ip_address = request.META.get('REMOTE_ADDR')
            term.tracking_id = stats.tracking_id(request)
            term.user = None
            if request.user.is_authenticated:
                term.user = request.user
            term.save()
        products = ProductsSearch(request.GET or None,
                                  queryset=Item.objects.all())
        if sort_by:
            results_qs = products.qs.order_by(sort_by)
        else:
            results_qs = products.qs.order_by('price')
        print(request.get_full_path)
        paginator = Paginator(results_qs, 1)

        page = request.GET.get('page')
        try:
            results = paginator.page(page)
        except PageNotAnInteger:
            results = paginator.page(1)
        except EmptyPage:
            results = paginator.page(paginator.num_pages)

        context = {
            'object_list': products,
            'sortform': sortForm,
            'page': page,
            'results': results
        }
        return render(request, "products/products.html", context)