Articles → DJANGO → List Page In Django

List Page In Django






Scenario







Example




  1. Create a function get_list in views.py in TestModule
  2. from django.http import HttpResponse
    from django.shortcuts import render
    from .models import MyNewScreen
    
    # Create your views here.
    def home(request):
        return render(request, "template1.html", {})
    
    
    def about(request):
        return render(request, "about.html", {})
    
    
    def get_list(request):
        template_name = "get_list.html"
        queryset = MyNewScreen.objects.all()
        context = {"list": queryset}
        return render(request, template_name, context)


  3. Create a HTML template
  4. <html>
        <body>
            <h1>My List</h1>
            <ul>
            {% for obj in list %}
                
                <li> {{ obj.name}} </li>
            {% endfor %}
        
            </ul>
        </body>
    </html>


  5. Add an entry in URLs.
  6. from django.contrib import admin
    from django.urls import path
    from TestModule.views import home
    from TestModule.views import about
    from TestModule.views import get_list
    
    urlpatterns = [
        path("admin/", admin.site.urls),
        path("", home),
        path("about", about),
        path("get_list", get_list),
    ]




Picture showing the output of the list page in Django
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, January 8, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250