简体   繁体   中英

django pagination is not working properly

i am trying to do pagination function based-view , it does work but...I m going to give you an example:

If i have a total of 100 results and i want to paginate by 50, it will give 2 pages, but both pages have same 100 results. Cause if i want to paginate by 50 it will give me 2 pages.

I used the the function based view from this link . i don't want classed view cause if i implement that the rest of the code would not work.

I m going to post a part of my code for this.

views.py

def search_form_table(request, pk):
    table_name = Crawledtables.objects.get(id=pk)
    t = create_model(table_name.name)
    if request.method == 'GET':
        form = AllTablesForm(request.GET)
        if form.is_valid():
           cd = form.cleaned_data
           title = cd['title']
           url = cd['url']
           query = t.objects.filter(title__icontains=title,
                                 url__icontains=url)

           page = request.GET.get('page', 1)

           paginator = Paginator(query, 50)
           try:
              users = paginator.page(page)
           except PageNotAnInteger:
              users = paginator.page(1)
           except EmptyPage:
              users = paginator.page(paginator.num_pages)

           return render(request, 'search_table.html', {'tbl_name': table_name,
                                                            'users': users,
                                                            'form': form,
                                                            'details': query})

I would try to replicate the nice example that you can see on the documentation (it is with a function-based view)!
https://docs.djangoproject.com/en/1.11/topics/pagination/#using-paginator-in-a-view

As you see page variable is equal to:

page = request.GET.get('page')  

instead of that you are doing

page = request.GET.get('page', 1)

Pass to request.GET's get() method only one string

I suggest to use get_page() in pagination.

Just try this:

def search_form_table(request, pk):
    table_name = Crawledtables.objects.get(id=pk)
    t = create_model(table_name.name)
    if request.method == 'GET':
        form = AllTablesForm(request.GET)
        if form.is_valid():
           cd = form.cleaned_data
           title = cd['title']
           url = cd['url']
           query = t.objects.filter(title__icontains=title,
                                 url__icontains=url)

           paginator = Paginator(query, 50)
           page = request.GET.get('page')
           users = paginator.get_page(page)

           return render(request, 'search_table.html', {'tbl_name': table_name,
                                                            'users': users,
                                                            'form': form,
                                                            'details': query})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM