Django 2.2 Python 3.7 ( ListView )
ListView Method Flowchart
1. setup() 2. dispatch() 3. http_method_not_allowed() 4. get_template_names() 5. get_queryset() 6. get_context_object_name() 7. get_context_data() 8. get() 9. render_to_response()
self.object_lsit
This is used in the html file. it will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon
get_context_data(**kwargs)
this function returns context data for displaying the list of objects Context: 1. 'object_list': The list of objects that this view is displaying. If 'context_object_name' is specified, that variable will also be set in the context, with the same value as 'object_list" 2. "is_paginated": A Boolean representing whether the results are paginated. Specifically, this is set to "False" if no page size has been specified, or the available objects do not span multiple pages. 3. 'paginator': An instances of django.core.paginator.Paginator. If the page is not paginated, this context varible will be 'None' 4. 'page_obj': An instance of django.core.paginator.Page. If tje page is not paginated, this context varable will be 'None'
example article_list.html
<h1>Articles</h1> <ul> {% for article in object_list %} ---<li>{{ article.pub_date|date }} - {{ article.headline }} </li> {% empty %} ---<li> No articles yet. </li> {% endfor %} </ul>
Pagination
Django provides a few classes that help you manage paginated data - that is, data that's split across several pages, with "Previous/Next" links. These classes live in
ListView Ancestors (MRO)
ListView inherits methods and attrubits from the following views: -django.views.generic.list.MultipleObjectTemplateResponseMixin -django.views.generic.base.TemplateResponseMixin -django.views.generic.list.BaseListView -django.views.generic.list.MultipleObjectMixin -djago.views.generic.base.View
ListView
ListView is a class located in django.views.generic.list.ListView This is for a page representing a list of objects while this view is executing, 'self.object_list' will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon
Paginator
This is a class that separates results on pages. from django.core.paginator import Paginator sample view.py def listing(request): --contact_list = Contacts.objects.all() paginator = Paginator(contact_list, 25) # show 25 contacts per page -- page = request.GET.get('page') --contacts = paginator.get_page(page) --return render( request, 'list.html', {'contacts': contacts}) sample list.html: {% for contact in contacts %} # each 'contact' is a Contact model object {{ contact.full_name|upper }} <br> #actually is name |( |=abs value key) upper {% endfor %} <div class='pagination'> -<span class='step-links'> -- {% if contacts.has_previous %} -----<a href='?page=1>« first</a> -----<a herf='?page={{ contacts.pervipis_page_number }}'> pervious</a> {% endif %} ----<span class="current"> ------Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.</span> -----{% if contacts.has_next %} ------<a href="?page={{ contacts.next_page_number }}">next</a> -------<a href="?page={{ contacts.paginator.num_pages }}">last »</a> ------{% endif %} -----</span> </div>
example urls.py
from django.urls import path from article.views import ArticleListView urlpattersn = [ ----path('', ArticleListView.as_view(), name = 'article-list'), ]
Example View.py
from django.utils import timezone from django.views.generic.list import ListView from articles.models import Article class ArticleListView(ListView): -model = Article -paginated_by = 100 # if pagination is desired -def get_context_data(self, **kwards): ---context=super().get_context_data(*kwards) ---context['now'] = timezone.now() ---return context