Listview – Using django class based view


List View refers to a view logic
to display multiple instances of a table in the database. django class based view provide an alternative way to implement views as Python objects instead of functions Based View. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:

Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.
Class based views are simpler and efficient to manage than function-based views. A function based view with tons of lines of code can be converted into a class based view with few lines only. This is where Object Oriented Programming comes into impact.

Django List View – To Function Based Views

A detailed Illustration ofHow to create and use List viewusing an Example. Consider a project named benchatronics having an app named myapp.

After we have created a project named benchatronics and an app named myapp, let’s create a model of which we will be creating instances through our view. In myapp/models.py,

# import the standard Django Model
# from built-in library
<strong>from</strong> django.db <strong>import</strong> models

# declare a new model with a name "BenchatronicsModel"
<strong>class</strong> <strong>BenchatronicsModel</strong>(models.Model):

# fields of the model
    name = models.CharField(max_lenght = 200)
title = models.CharField(max_length = 200)
description = models.TextField()

# renames the instances of the model
# with their title name
<strong>def</strong> <strong>__str__</strong>(self):
<strong>return</strong> self.title

After creating our model, we need to run two commands in order to create Database for the model.

Python manage.py makemigrations
Python manage.py migrate
<strong>from</strong> django.views.generic.list <strong>import</strong> ListView
<strong>from</strong> .models <strong>import</strong> *

<strong>class</strong> <strong>BennyList</strong>(<strong>ListView</strong>):

# specify the model for list view
model = BenchatronicsModel

Now create a url path to map the view. In myapp/urls.py

<strong>from</strong> django.urls <strong>import</strong> path

# importing views from views.py
<strong>from</strong> .views <strong>import</strong> BennyList
#or you use from .views import *
urlpatterns = [
path('', BennyList.as_view()),
]

Create a template in templates/myapp/benny_list.html,

<ul>
<!-- Iterate over object_list -->
{% <strong>for</strong> object <strong>in</strong> object_list %}
<!-- Display Objects -->
    <li>{{object.name}}</li>
<li>{{ object.title }}</li>
<li>{{ object.description }}</li>

<hr/>
<!-- If object_list <strong>is</strong> empty -->
{% empty %}
<li>No objects yet.</li>
{% endfor %}
</ul>
class base view

Now we have created our model and everything is working fine and we have created url that mapped the model.

Leave a Reply

Your email address will not be published. Required fields are marked *