Valachio
Valachio

Reputation: 1135

Django - Paginate with another GET request; not working with just pagination page number

I'm building a page with pagination and a filter form (2 GET requests). If the URL includes both pagination and filter results, something like /questions/?page=2&all_questions=on, it works fine. It also works if it just has filter results, something like /questions/?all_questions=on.

However if it only has the pagination page result, something like /questions/?page=1, no results are shown.

So I figured I need to do something with the views so that if there is only a page number in the URL, a default filter will be given. I know I probably need to add something to the Try and Except section of pagination, but I'm stumped as to the actual code I need to write.

def questions_index(request):

    user = request.user
    form = QuestionFilterForm(request.GET or None)
    question_list = []
    if not form.is_bound:
        question_list = Question.objects.all().order_by('-date_created')
    if form.is_valid():
        if form.cleaned_data['all_questions'] | (form.cleaned_data['general_questions'] & form.cleaned_data['location_all_gta']) == True:
            question_list = Question.objects.all().order_by('-date_created')
        elif form.cleaned_data['location_all_gta'] == True:
            question_list += Question.objects.filter(question_type=1).order_by('-date_created')
        else:
            if form.cleaned_data['general_questions'] == True:
                question_list += Question.objects.filter(question_type=2).order_by('-date_created')
            if form.cleaned_data['location_toronto'] == True:
                question_list += Question.objects.filter(location='1').order_by('-date_created')

    paginator = Paginator(question_list, 15)

    # Pagination
    page = request.GET.get('page')
    try:
        questions = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        questions = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        questions = paginator.page(paginator.num_pages)

    ### I need to write something here...
    except (url has no filter result)  
        give default filter 

    return render(request, 'questions_index.html', {'questions': questions, 'user': user, 'form': form})

Upvotes: 1

Views: 316

Answers (1)

Daniel Coelho
Daniel Coelho

Reputation: 456

Your problem is at the beginning, not at the end. When you write:

if not form.is_bound:
    question_list = Question.objects.all().order_by('-date_created')

This condition will only be met if request.GET is empty. The is_bound method will return True if you give any data to it, no matter if the keys of the dictionary are not form fields (docs). In the case you described, request.GET will contain the page key only.

A possible solution is to rewrite this part as:

if not form.is_bound or (len(request.GET) == 1 and 'page' in request.GET):
    question_list = Question.objects.all().order_by('-date_created')

Upvotes: 4

Related Questions