Articles → DJANGO → Listview In Django
Listview In Django
Implementation
- In views.py, add the following code
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()
- Add following code in url.py
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()),
]
- Add following code in get_list.html
<html>
<body>
<h1>My List</h1>
<ul>
{% for obj in object_list %}
<li> {{ obj.name}} </li>
{% endfor %}
</ul>
</body>
</html>
Output
Click to Enlarge