Answers for "how to use listview in django"

2

importying listviewin django

from django.views.generic import ListView,TemplateView
from django.views.generic.base import View
from django.views.generic.detail import DetailView
Posted by: Guest on August-04-2021
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>
Posted by: Guest on December-21-2020

Python Answers by Framework

Browse Popular Code Answers by Language