src/products/views.py
def products_list(request):
    """
    Renders the products/product_list.html template which lists all the
    currently available polls
    """
    if not request.GET._mutable:
        request.GET._mutable = True
    products = Product.objects.get_queryset().order_by('id')
    
    EXCLUSIVE_QUERIES = [
        'title',
        'pub_date',
        'view_count',
        'price_increasing',
        'price_descending',
        'condition_used',
        'condition_new',
        'multiple',
        'unique',
    ]
    count = 0
    for query in EXCLUSIVE_QUERIES:
        if query in request.GET:
            count +=1
    
    print(count, '-number of queries')
    #sorting
    if 'title' in request.GET:
        products = products.order_by('title')

    elif 'pub_date' in request.GET:
        products = products.order_by('-pub_date')

    elif 'view_count' in request.GET:
        products = products.order_by('-view_count')

    elif 'price_increasing' in request.GET:
        products = products.order_by('price_in_SGD')

    elif 'price_descending' in request.GET:
        products = products.order_by('-price_in_SGD')

    elif 'condition_used' in request.GET:
        products = products.filter(condition__exact = 'USED') #used items

    elif 'condition_new' in request.GET:
        products = products.filter(condition__exact = 'N') #new items 

    elif 'multiple' in request.GET:
        products = products.filter(this_product_has_multiple_quantities__exact = True) 

    elif 'unique' in request.GET:
        products = products.filter(this_product_has_multiple_quantities__exact = False)
    ###
    #search bar
    search_term = request.GET.get('search_term')
    if search_term:
        products = products.filter(title__icontains=search_term)
        
    #pagination:
    LISTINGS_PER_PAGE = 8
    paginator = Paginator(products, LISTINGS_PER_PAGE)

    page = request.GET.get('page')
    products = paginator.get_page(page) #specific chunk of products

    ###Preserving Query Parameters When Using Paginator
    get_dict_copy = request.GET.copy()
    params = get_dict_copy.pop('page', True) and get_dict_copy.urlencode()
    print('----------')
    print(params)
    print('----------')

    number_of_total_items = Product.objects.count()
    context = {'posts': products, 'params': params, 'search_term': search_term,
               'number_of_total_items': number_of_total_items,
               'number_of_filtered_items': len(products)}
    ###
    return render(request, 'products/product_list.html', context)

    Renders the products/product_list.html template which lists all the
    currently available polls
    
Renders the products/product_list.html template which lists all the
currently available polls
Renders the products / product_list . html template which lists all the currently available polls
def products_list(request):
    if not request.GET._mutable:
        request.GET._mutable = True
    products = Product.objects.get_queryset().order_by('id')
    EXCLUSIVE_QUERIES = [
        'title',
        'pub_date',
        'view_count',
        'price_increasing',
        'price_descending',
        'condition_used',
        'condition_new',
        'multiple',
        'unique',
    ]
    count = 0
    for query in EXCLUSIVE_QUERIES:
        if query in request.GET:
            count +=1
    print(count, '-number of queries')
    if 'title' in request.GET:
        products = products.order_by('title')
    elif 'pub_date' in request.GET:
        products = products.order_by('-pub_date')
    elif 'view_count' in request.GET:
        products = products.order_by('-view_count')
    elif 'price_increasing' in request.GET:
        products = products.order_by('price_in_SGD')
    elif 'price_descending' in request.GET:
        products = products.order_by('-price_in_SGD')
    elif 'condition_used' in request.GET:
        products = products.filter(condition__exact = 'USED') 
    elif 'condition_new' in request.GET:
        products = products.filter(condition__exact = 'N') 
    elif 'multiple' in request.GET:
        products = products.filter(this_product_has_multiple_quantities__exact = True) 
    elif 'unique' in request.GET:
        products = products.filter(this_product_has_multiple_quantities__exact = False)
    search_term = request.GET.get('search_term')
    if search_term:
        products = products.filter(title__icontains=search_term)
    LISTINGS_PER_PAGE = 8
    paginator = Paginator(products, LISTINGS_PER_PAGE)
    page = request.GET.get('page')
    products = paginator.get_page(page) 
    Preserving Query Parameters When Using Paginator
    get_dict_copy = request.GET.copy()
    params = get_dict_copy.pop('page', True) and get_dict_copy.urlencode()
    print('----------')
    print(params)
    print('----------')
    number_of_total_items = Product.objects.count()
    context = {'posts': products, 'params': params, 'search_term': search_term,
               'number_of_total_items': number_of_total_items,
               'number_of_filtered_items': len(products)}
    return render(request, 'products/product_list.html', context)
def products_list ( request ) : if not request . GET . _mutable : request . GET . _mutable = True products = Product . objects . get_queryset ( ) . order_by ( 'id' ) EXCLUSIVE_QUERIES = [ 'title' , 'pub_date' , 'view_count' , 'price_increasing' , 'price_descending' , 'condition_used' , 'condition_new' , 'multiple' , 'unique' , ] count = 0 for query in EXCLUSIVE_QUERIES : if query in request . GET : count += 1 print ( count , '-number of queries' ) if 'title' in request . GET : products = products . order_by ( 'title' ) elif 'pub_date' in request . GET : products = products . order_by ( '-pub_date' ) elif 'view_count' in request . GET : products = products . order_by ( '-view_count' ) elif 'price_increasing' in request . GET : products = products . order_by ( 'price_in_SGD' ) elif 'price_descending' in request . GET : products = products . order_by ( '-price_in_SGD' ) elif 'condition_used' in request . GET : products = products . filter ( condition__exact = 'USED' ) elif 'condition_new' in request . GET : products = products . filter ( condition__exact = 'N' ) elif 'multiple' in request . GET : products = products . filter ( this_product_has_multiple_quantities__exact = True ) elif 'unique' in request . GET : products = products . filter ( this_product_has_multiple_quantities__exact = False ) search_term = request . GET . get ( 'search_term' ) if search_term : products = products . filter ( title__icontains = search_term ) LISTINGS_PER_PAGE = 8 paginator = Paginator ( products , LISTINGS_PER_PAGE ) page = request . GET . get ( 'page' ) products = paginator . get_page ( page ) get_dict_copy = request . GET . copy ( ) params = get_dict_copy . pop ( 'page' , True ) and get_dict_copy . urlencode ( ) print ( '----------' ) print ( params ) print ( '----------' ) number_of_total_items = Product . objects . count ( ) context = { 'posts' : products , 'params' : params , 'search_term' : search_term , 'number_of_total_items' : number_of_total_items , 'number_of_filtered_items' : len ( products ) } return render ( request , 'products/product_list.html' , context )
Renders the products/product_list.html template which lists all the
currently available polls
Renders the products / product_list . html template which lists all the currently available polls
"""
    Renders the products/product_list.html template which lists all the
    currently available polls
    """ #sorting #used items #new items  ### #search bar #pagination: #specific chunk of products ###Preserving Query Parameters When Using Paginator ###
request