Introduction to Django's Class based views - Understanding how a class based view works

Reading Time : ~ .

Django has MVT architecture. A view in Django is just a callable that takes a request and returns a response. But this can be more than just a function, that contains the actual business logic of an URL. In addition to normal function based views Django provides of some classes which can be used as views. These allow you to structure your views and reuse code by inheriting them. In the current blog post let us see how a Class Based view work. Using this explanation we can easily override any function of built in generic class based views.  For this we’ll see a basic view.

In views.py

from django.http import HttpResponse
from django.views.generic import View

class TestView(View):
     
    def get(self, request, *args, **kwargs):
         return HttpResponse('Hello, World!')

In urls.py

from django.conf.urls import url
from testapp.views import TestView
urlpatterns = [
    url(r'^hello-world/$', TestView.as_view(), name='hello_world'),
]

When we type the url in the browser ‘http://locahost:8000/hello-world/’, it would be dispatched to the corresponding view to its as_view() function.
The as_view() function will call the dispatch() function.

dispatch():

This function validates if the request method i.e GET or POST or any other is in allowed methods of the view by checking http_method_names attribute. If the request is in corresponding method then it will dispatch the request to corresponding method. In our case as it is get request it calls get(request, *args, **kwargs). If the request method is not in http_method_names then it will call http_method_not_allowed method.

http_method_not_allowed:

This method’s default implementation returns HttpResponseNotAllowed with a list of allowed methods in plain text.

This is simple explanation of how a class based view work, we'll learn more of Different kinds of View classes and their usages in coming blog posts.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Understanding django serializers with examples Vamsi Popuri

Serializers are used for “translating” Django models into other formats like xmi,json,yaml(YAML Ain’t a Markup Language)

from django.core import serializers
data = serializers.serialize("xml", SomeModel.objects.all())

Continue Reading...
E-commerce (paypal) integration with Django Ashwin Kumar

E-commerce is integration is becoming almost essential for every web application now a days. There are so many payment gateways to integrate with our application. ...

Continue Reading...
How to index binary files in django haystack Ramya Ambati

Now we are going to index text content which is stored in structured files such as PDFs, Microsoft Office documents, images, etc using haystack and ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties