Articles → DJANGO → Detail View In Django

Detail View In Django






Implementation




  1. In views.py, create a class for detail view
  2. from django.http import HttpResponse
    from django.shortcuts import render
    from models import MyNewScreen
    from django.views.generic import ListView
    from django.views.generic import DetailView
    
    
    class MyListView(ListView):
        template_name = 'get_list.html'
        queryset = MyNewScreen.objects.all()
    
    
    class MyDetailView(DetailView):
        template_name = 'detail.html'
        queryset = MyNewScreen.objects.all()


  3. Create a HTML template for details view
  4. <h1>{{object.name}}</h1>


  5. Add an entry in urls.py
  6. from django.contrib import admin
    from django.urls import path
    from TestModule.views import MyListView, MyDetailView
    
    urlpatterns = [path('admin/', admin.site.urls), path('mylist',
                   MyListView.as_view()), path('<int:pk>/',
                   MyDetailView.as_view())]



Output


Picture showing the output of detail view in django
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, January 21, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250