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 in the views so that if there is only 
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 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})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2a2f05ae-d71a-419b-9df0-394e1e129fcb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to