Views In Django – Free Python Tutorials and Online Training

What are views in Django?

A view is simply a python function that takes input as a web request and returns a web response as a output.This output as a web response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, etc.

A view is a place where the users will put the logic of their application.

A view in django is an endpoint that can be accessed by any client to retrieve data.For example view server JSON data to a web server and these views becomes parts of RESTful API.While other view  serve data rendered with a template to a web server that in turn serves the HTML page to a client.

Views in django are simple python functions or methods which return data to a front-end web server.When a request comes in,django will choose a view by examining the request’s URL after the domain name.


Types Of Views In Django

There are two types of views in django
1] Class-Based Views(CBVs)
2] Function-Based Views(FBVs)

Originally first there were only function based views, class based views in django are created after that so that you don’t have to write same code over and over again.Class based views also provide a way so that you don’t have to write much code.We will use both function based and class based views in our blog app as per requirements.

As their core,class based views are python classes.Django provides a variety of template class based views that have a pre-configured functionality that user can reuse.Based on what kind of functionality these classes are providing these classes are then given a helpful names.
for example “generic views” provide solutions to common requirements.


1] Class-Based Views

If user created a view extending the django.views.View base class, the dispatch() method will handle the HTTP method logic. If the request is a POST, it will execute the post() method inside the view, if the request is a GET, it will execute the get() method inside the view.

Class-Based Views Example

urls.py file will  contain this:

urlpatterns = [
    url(r'contact/$', views.BlogView.as_view(), name='blog'),
]

Views.py file corresponding to that urls.py file will contain this:

from django.views import View

class BlogView(View):
    def get(self, request):
        # Code block for GET request

    def post(self, request):
        # Code block for POST request

more information about class based views can be found here https://docs.djangoproject.com/en/2.0/topics/class-based-views/


2] Function-Based Views

Function-based views are simple than class-based views.In function-based views, this logic is handled with if statements:

urls.py file will  contain this:

urlpatterns = [
    url(r'contact/$', views.contact, name='contact'),
]

Views.py file corresponding to that urls.py file will contain this:

def contact(request):
    if request.method == 'POST':
        # Code block for POST request
    else:
        # Code block for GET request (will also match PUT, HEAD, DELETE, etc)

Those are the main differences between class-based views and function-based views in django.more information about function-based views in django can be found here https://docs.djangoproject.com/en/2.0/topics/http/views/

We are also creating function-based views in our post “Writing your first blog app-part 2“.


Generic Class-Based Views

To address the common use cases in web application such as form handling,list views “generic class-based views” were introduced.

We can implement them from the module django.views.generic

Here is an overview of the available views:

Simple Generic Views

  • View
  • TemplateView
  • RedirectView

List Views

  • ListView

Editing Views

  • FormView
  • CreateView
  • UpdateView
  • DeleteView

This is all about Views in django and their different types.

Leave a Comment

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