Django Detail View – Creating Views Using Class Based View

Django detail views

Below are illustration ofHow to Create a Class Based View In Django

A Django view is callable which takes a request and returns a response. This is when dynamic rendering comes in place. This Class Based View can be more than just a function, and Django provides an example of some classes which can be used as views.

There are two main popular views I know in django:

  • FUNCTION BASED VIEW
  • CLASS BASED VIEW

In this article we will be talking about Class Base View

Class Based View allows you to structure your views and reuse code by harnessing inheritance and mixins.

What is django class base view?

Class-based view is analternate way of function-based view. In the beginning, there was only function-based view in Django: “Django passed your function an HttpRequest and expected back an HttpResponse.”

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views.

SOME IMPORTANTS OF CLASE BASED VIEW

  • Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
  • Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.

Class based views are simpler and efficient to manage than function-based views. A function based view with tons of lines of code can be converted into a class based views with few lines only. This is where Object Oriented Programming comes into impact.

Django DetailView – Class Based Views

Illustration ofHow to create and use Detail viewusing an Example. Let’s consider a project named benchatronicshaving an app named myapp.

After you have a project and an app, let’s create a model of which we will be creating instances through our view. In myapp/models.py,

# import the standard Django Model
# from built-in library
<strong>from</strong> django.db <strong>import</strong> models
   
# declare a new model with a name "BenchatronicsModel"
<strong>class</strong> <strong>BenchatronicsModel</strong>(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
    <strong>def</strong> <strong>__str__</strong>(self):
        <strong>return</strong> self.title

After 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

To keep codes simple let go to our myapp/admin.py then register our model.

We need to import our model then register it, check the code below.

<strong>from</strong> django.contrib <strong>import</strong> admin
<strong>from</strong> . models <strong>import</strong> *

# Register your models here.
<strong>class</strong> <strong>BenchatronicsAdmin</strong>(admin.ModelAdmin):
    list_display = ('title',)

admin.site.register(Benchatronics,BenchatronicsAdmin)

Now we have to create a superuser.

illustration ofhow to create a superuser in django.

python manage.py createsuperuser
#enter your username, if you leave it blank it will use your device username
#enter email,you can leave it blsnk
#enter your password" confirm it

Now to verify the instances we have created login into your admin data base using127.0.0.1/8000

In Class Based Views all details are automatically setup for us; I mean everything from A to Z.What One just needs is to specify which model to create DetailView for, then Class based DetailView will automatically try to find a template in.

For example in the project you have created say for benchatronics we will be creating detail view and single view in myapp/views.py.

<strong>from</strong> django.views.generic.detail <strong>import</strong> DetailView
  
<strong>from</strong> .models <strong>import</strong> BenchatronicsModel
  
<strong>class</strong> <strong>BenchtDetailView</strong>(<strong>DetailView</strong>):
    # specify the model to use
    model = BenchatronicsModel

Now lets create a url path to map the view. In benchatronics/urls.py

<strong>from</strong> django.urls <strong>import</strong> path
  
# importing views from views.py
<strong>from</strong> .views <strong>import</strong> BenchtDetailView
urlpatterns = [
    # <pk> is identification for id field,
    # slug can also be used
    path('<pk>/', BenchtDetailView.as_view()),
]

Let’s create a template intemplates/benchatronics/benchatronics/model_detail.html,

Create a template in your own project if you don’t know how to do that you can google it now

<<strong>h1</strong>>Title: {{ object.title }}</<strong>h1</strong>>
<<strong>p</strong>>This is the description of the model {{ object.description }}</<strong>p</strong>>

Now you have to check it out on this local host by running python manage.py runserverhttp://localhost:8000/1/

Leave a Reply

Your email address will not be published. Required fields are marked *