Detail View - Function Based Views Django - GeeksforGeeks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

Write & Earn DSA Data Structures Algorithms Interview Preparation Data Science Topic

Detail View – Function based Views Django


Last Updated :
27 Aug, 2021

Read Discuss Practice Video Courses

Detail View refers to a view (logic) to display a par ticular instance of a table from the

database with all the necessar y details. It is used to display multiple types of data on

a single page or view, for example, profile of a user. Django provides extra-ordinar y

suppor t for Detail Views but let ’s check how it is done manually through a function-

based view. This ar ticle revolves around Detail View which involves concepts such as

Django Forms, Django Models. 

For Detail View, we need a project with some models and multiple instances which

will be displayed.

Django Detail View – Function Based Views

Illustration of How to create and use Detail view using an Example. Consider a

project named geeksforgeeks having an app named geeks. 

Refer to the following ar ticles to check how to create a project and an app in

Django. 

How to Create a Basic Project using MV T in Django?

How to Create an App in Django ?

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Af ter you have a project and an app, let ’s create a model of which we will be creating
Got It !
instances through our view. In geeks/models.py, 

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 1/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

Start Your Coding Journey Now!


 

Login Register
AD -44% -63% -40% -32% -35% -69% -3

Tai nghe không … Máy lọc không … Tai nghe không … Tai nghe Blueto… Tai nghe Blueto… Tai nghe Blueto… T

250.000₫ 1.090.000₫ 890.000₫ 2.690.000₫ 4.490.000₫ 690.000₫ 4

P ython3

# import the standard Django Model


# from built-in library
from django.db import models
  
# declare a new model with a name "GeeksModel"
class GeeksModel(models.Model):
 
    # fields of the model
    title = models.CharField(max_length = 200)
    description = models.TextField()
 
    # renames the instances of the model
    # with their title name
    def __str__(self):
        return self.title

Af ter creating this model, we need to run two commands in order to create Database

for the same. 

Python manage.py makemigrations

Python manage.py migrate

Now let ’s create some instances of this model using shell, run form bash, 

Python manage.py shell

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Enter following commands 

  Got It !

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 2/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

Start Your Coding Journey Now!


>>> from geeks.models import GeeksModel
Login Register
>>> GeeksModel.objects.create(

title="title1",

description="description1").save()

>>> GeeksModel.objects.create(

title="title2",

description="description2").save()

>>> GeeksModel.objects.create(

title="title3",

description="description3").save()

Now we have ever ything ready for back end. Verif y that instances have been created

from http://localhost:8000/admin/geeks/geeksmodel/ 

For detail_view one would need some identification to get a par ticular instance of the

model. Usually it is unique primar y key such as id. To specif y this identification we

need to define it in urls.py. Go to geeks/urls.py, 

P ython3

from django.urls import path


 
# importing
We views
use cookies to ensure you from
have theviews..py
best browsing experience on our website. By using our site, you
acknowledge
from .views import that you have read and understood our
Cookie Policy &
Privacy Policy
detail_view
 
Got It !
urlpatterns = [

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 3/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

    path('<id>', detail_view ),
Start Your Coding Journey Now!
] Login Register

Let ’s create a view and template for the same. In geeks/views.py,

P ython3

from django.shortcuts import render


 
# relative import of forms
from .models import GeeksModel
 
# pass id attribute from urls
def detail_view(request, id):
    # dictionary for initial data with
    # field names as keys
    context ={}
 
    # add the dictionary during initialization
    context["data"] = GeeksModel.objects.get(id = id)
         
    return render(request, "detail_view.html", context)

Create a template in templates/Detail_view.html, 

html

<div class="main">
     
    <!-- Specify fields to be displayed -->
    {{ data.title }}<br/>
    {{ data.description }}<br/>
 
</div>

Let ’s check what is there on http://localhost:8000/1 

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 4/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

Start Your Coding Journey Now! Login Register

Bingo..!! Detail view is working fine. One can also display selected fields according to

type of usage required in multiple forms. Of ten it is not the id which is used to define

the detail view it is the slug. To know more about slug and SlugField visit – Add the

slug field inside Django Model

Like 9

Previous Next

Related Articles

1. Create View - Function based Views Django

2. Update View - Function based Views Django


We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !
3. Delete View - Function based Views Django
https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 5/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

Start
4. Your Coding
List View Journey
- Function Now!Django
based Views Login Register

5. Class Based vs Function Based Views - Which One is Better to Use in


Django?

6. Django CRUD (Create, Retrieve, Update, Delete) Function Based


Views

7. Function based Views - Django Rest Framework

8. UpdateView - Class Based Views Django

9. Class Based Generic Views Django (Create, Retrieve, Update, Delete)

10. Class based views - Django Rest Framework

Ar ticle Contributed By :

NaveenArora
@NaveenArora

Vote for difficulty

Easy Normal Medium Hard Expert

Improved By : ramanratnakar, sagar0719kumar

Article Tags : Django-views, Python Django, Python

Practice Tags : python


We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Improve Article Report Issue Got It !

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 6/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

Start Your Coding Journey Now! Login Register

A-143, 9th Floor, Sovereign Corporate Tower,

Sector-136, Noida, Uttar Pradesh - 201305


[email protected]

Company Learn
About Us DSA
Careers Algorithms
In Media Data Structures
Contact Us SDE Cheat Sheet
Privacy Policy Machine learning
Copyright Policy CS Subjects
Advertise with us Video Tutorials
Courses

News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
Golang
Finance
C#
Lifestyle
SQL
Knowledge
Kotlin

Web Development Contribute


Web Tutorials Write an Article
Django Tutorial Improve an Article
HTML Pick Topics to Write
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
JavaScript Write Interview Experience
that you have read and understood our
Cookie Policy &
Privacy Policy
Bootstrap Got It ! Internships

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 7/8
12/3/22, 8:37 PM Detail View - Function based Views Django - GeeksforGeeks

ReactJS Video Internship


Start Your Coding
NodeJS Journey Now!
Login Register

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/detail-view-function-based-views-django/ 8/8

You might also like