list view django code example

Example 1: django listview

class YourView(ListView):
	model				= YourModel
    paginate_by			= your_pagination_number
    context_object_name = 'your_model_in_plural'
    template_name 		= 'your_list.html'

Example 2: listview django

#views.py
#GeeksModel is a example model
from django.views.generic.list import ListView 
from .models import GeeksModel 
  
class GeeksList(ListView): 
  	paginate_by=3
    # specify the model for list view 
    model = GeeksModel
    
#Now create a url path to map the view. In geeks/urls.py,

from django.urls import path 
  
# importing views from views..py 
from .views import GeeksList 
urlpatterns = [ 
    path('', GeeksList.as_view()), 
] 

#in your template you can manipulate pagination 
{% for contact in page_obj %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br>
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if page_obj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>

        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
            <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

Example 3: django display view library

from django.views.generic.detail import DetailView