chris26jp
chris26jp

Reputation: 27

django pagination error: AttributeError: 'WSGIRequest' object has no attribute 'Get'

I am having a hard time with django pagination. Can someone help me out with this?

This view is returning an error:

def view_homePage(request, user):
if user == 'all':
    posts = Post.objects.order_by("-timestamp").all()
    paginator = Paginator(posts, 2)
        
    ERROR ---> page_number = request.Get.get('page') or 1
    page_obj = paginator.get_page(page_number)
    return JsonResponse([post.serialize() for post in page_obj], safe=False)

The error I am getting is: AttributeError: 'WSGIRequest' object has no attribute 'Get'

If I remove the line and just set page_number = 1 for testing I have a new set of issues. How do I actually pass the page number to the view from the html page? I tried adding this for testing, but it does not work:

<nav aria-label="...">
     <ul class="pagination">
         <li class="page-item"><a class="page-link" 
               href="?page=3">3</a>
         </li>
     </ul>
 </nav>

On the above code I hard coded page 3 just for testing, but it does not get into the view. How do I go about this? The django documentation is lacking in this regard.

Upvotes: 1

Views: 620

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477318

The querystring is stored in a QueryDict in the .GET attribute [Django-doc] of the request, so:

page_number = request.GET.get('page') or 1

Note that Django's identifiers are case sensitive, so .GET and .Get are different.

Upvotes: 2

Related Questions