Articles → DJANGO → List Page In Django
List Page In Django
Scenario
Example
- Create a function get_list in views.py in TestModule
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)
- Create a HTML template
<html>
<body>
<h1>My List</h1>
<ul>
{% for obj in list %}
<li> {{ obj.name}} </li>
{% endfor %}
</ul>
</body>
</html>
- Add an entry in URLs.
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),
]
Click to Enlarge