How to get page in django?

0

Using Django. I have info on amount of page in database this amount is entered by each user and stored in bd.  my concern is: How can I page with the amount I have stored in bd, for example, change 25 by the number I have in my database that I have in database?

class Datos(ListView):
    model = Datos
    template_name = 'Datos.html'
    paginate_by = 25

In my model:

class ProfileUser(models.Model):
    pagination = models.IntegerField(default=50)

Thanks

    
asked by NEFEGAGO 05.05.2018 в 23:02
source

2 answers

1

One way to modify it is to modify the variable paginate_by within the function get_context_data() but the most recommendable thing is to overwrite the function get_queryset() in this way:

from django.core.paginator import Paginator

def get_queryset(self,npagina=1):
    consulta = Datos.objects.all()
    paginacion = Paginator(consulta,'cantidad objetos')
    if len(paginacion.page(1)) == 0:
        return None
    else:
        return paginacion.page(npagina)

You must modify your get_context_data to obtain the result and pass it to your template adding this:     context ['data'] = self.get_queryset (npagina = self.request.GET.get ('page'))

And finally add this your tamplate:

{% if is_paginated %}
    <ul class="pagination">
    {% if datos.has_previous %}
        <li>
            <span><a href="?page={{ page_obj.previous_page_number }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}">Anterior</a></span>
        </li>
    {% endif %}
        <li class="">
            <span>Página {{ page_obj.number }} de {{ page_obj.paginator.num_pages }}.</span>
        </li>
    {% if datos.has_next %}
        <li>
            <span><a href="?page={{ page_obj.next_page_number }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}">Siguiente</a></span>
        </li>
    {% endif %}
    </ul>
{% endif %}

You can modify the function of get_queryset to your liking, it's just an example so you can modify it the way you want

    
answered by 07.05.2018 / 09:23
source
0

Good morning I hope you are well, I wanted to ask for help since I am in the same situation. In context ['data'], 'data' what would it be? I'm just starting in Django so I do not have much experience, thank you very much

    
answered by 18.06.2018 в 17:55