Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Detail View - Function based Views Django
Next article icon

List View - Function based Views Django

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A List View is a type of Django view used to display multiple instances of a model (i.e., rows from a database table). It is commonly used when you need to present a list of items on a page, for example, listing products on an eCommerce site. While Django provides built-in support for List Views using class-based views, this guide demonstrates how to implement it manually using function-based views.

Django List View - Function Based Views

Illustration of How to create and use List view using an example, consider a project named "geeksforgeeks" having an app named "geeks". 

Refer to the following articles to check how to create a project and an app in Django:

  • How to Create a Basic Project using MVT in Django?
  • How to Create an App in Django ?

Step 1: Define a Model

Create a simple model in geeks/models.py:

Python
from django.db import models  class GeeksModel(models.Model):     title = models.CharField(max_length=200)     description = models.TextField()      def __str__(self):         return self.title 

Step 2: Apply Migrations

Run the following commands to create the corresponding database table

python manage.py makemigrations
python manage.py migrate

Step 3: Create Sample Data

Use Django's shell to create model instances:

python manage.py shell

Enter following commands in shell :

from geeks.models import GeeksModel

GeeksModel.objects.create(title="title1", description="description1")
GeeksModel.objects.create(title="title2", description="description2")
GeeksModel.objects.create(title="title3", description="description3")

django_shell1
Snapshot of the Command in Shell

Now we have everything ready for back end.

To verify that instances have been created, exit the shell using exit() command and run the development server using this command:

python manage.py runserver

After running the commad, visit the deelopment url- http://127.0.0.1:8000/

django-listview-check-models-instances

Step 4: Create the List View

In geeks/views.py, define a function-based view to fetch and render the model instances:

Python
from django.shortcuts import render from .models import GeeksModel  def list_view(request):     context = {         "dataset": GeeksModel.objects.all()     }     return render(request, "list_view.html", context) 

Step 5: Create the Template

In templates/list_view.html, add the following HTML to display the data:

html
<div class="main">     {% for data in dataset %}         <h3>{{ data.title }}</h3>         <p>{{ data.description }}</p>         <hr/>     {% endfor %} </div> 

Run the development server and visit the URL http://127.0.0.1:8000/ to see the list view in action.

Output:

django-listview-function-based

Sorting the List (Descending Order)

You can order the results in reverse (newest first) by modifying the view:

Python
def list_view(request):     context = {         "dataset": GeeksModel.objects.all().order_by("-id")     }     return render(request, "list_view.html", context) 

order_by("-id"): sorts the items in descending order of their ID.

Output:

django-listview-order-by

Filtering the List

You can also filter the data shown in the list view. For example, let’s add a new entry:

Lunch the shell using the below command:

python manage.py shell 

Enter the following commands in shell:

from geeks.models import GeeksModel

GeeksModel.objects.create(title = "Naveen", description = "GFG is Best").save()

django-listview-models

To display only items with the word “title” in their title, update the view:

Python
def list_view(request):     context = {         "dataset": GeeksModel.objects.filter(title__icontains="title")     }     return render(request, "list_view.html", context) 

title__icontains="title" filters entries whose title contains the word "title", case-insensitively.

Run the development server and visit the URL http://127.0.0.1:8000/ again to see the filtered output.

django-listview-function-based2

Next Article
Detail View - Function based Views Django

N

NaveenArora
Improve
Article Tags :
  • Python
  • Python Django
  • Django-views
Practice Tags :
  • python

Similar Reads

    Django Function Based Views
    Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. Django is based on MVT (Model View Template) architecture and revolves around CRUD (Create, Retrieve, Up
    7 min read
    Create View - Function based Views Django
    Create View refers to a view (logic) to create an instance of a table in the database. It is just like taking an input from a user and storing it in a specified table. Django provides extra-ordinary support for Create Views but let's check how it is done manually through a function-based view. This
    3 min read
    List View - Function based Views Django
    A List View is a type of Django view used to display multiple instances of a model (i.e., rows from a database table). It is commonly used when you need to present a list of items on a page, for example, listing products on an eCommerce site. While Django provides built-in support for List Views usi
    3 min read
    Detail View - Function based Views Django
    Detail View refers to a view (logic) to display a particular instance of a table from the database with all the necessary details. It is used to display multiple types of data on a single page or view, for example, profile of a user. Django provides extra-ordinary support for Detail Views but let's
    3 min read
    Update View - Function based Views Django
    Update View refers to a view (logic) to update a particular instance of a table from the database with some extra details. It is used to update entries in the database for example, updating an article at geeksforgeeks. So Update view must display the old data in the form and let user update the data
    4 min read
    Delete View - Function based Views Django
    Delete View refers to a view (logic) to delete a particular instance of a table from the database. It is used to delete entries in the database for example, deleting an article at geeksforgeeks. So Delete view must show a confirmation message to the user and should delete the instance automatically.
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences