Articles → DJANGO → Listview In Django

Listview In Django






Implementation




  1. In views.py, add the following code
  2. from django.http import HttpResponse
    from django.shortcuts import render
    from .models import MyNewScreen
    from django.views.generic import ListView
    
    
    class MyListView(ListView):
        template_name = "get_list.html"
        queryset = MyNewScreen.objects.all()


  3. Add following code in url.py
  4. from django.contrib import admin
    from django.urls import path
    from TestModule.views import (
        MyListView,
    )
    
    urlpatterns = [
        path("admin/", admin.site.urls),
        path("mylist", MyListView.as_view()),
    ]


  5. Add following code in get_list.html
  6. <html>
        <body>
            <h1>My List</h1>
            <ul>
            {% for obj in object_list %}
                
                <li> {{ obj.name}} </li>
            {% endfor %}
        
            </ul>
        </body>
    </html>

Output


Picture showing the output of Listview in django
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, January 18, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250