Django View

In django app view contains all methods which used to link with url and display on local web browser.

There are two types of view which used to develop any web application:

1-Class based views

from django.http import HttpResponse

from django.views import View

class MyView(View):

      def get(self, request):

            # <view logic>

            return HttpResponse('result')

At the starting django start with function based view but a now it also included class based views but it not take place of function based views. Main reason to create class based view is that when we perform the CURD operation therefore this code repeats unnecessarily, then creating of Generic Class-based views.

2-Function based views

from django.http import HttpResponse

 

def Hello(request):

   return HttpResponse("Hello")

or

from django.http import HttpResponse

def my_view(request):

       if request.method == 'GET':

              # <view logic>

              return HttpResponse('result')

A View function, or view for short, is a Python functions that takes a Web request and gives a Web response. This response is the HTML contents of a Web page, or an XML document, or a redirect, or a 404 error, or an image. The View itself consists of whatever arbitrary logic requires to return that response.

Powered  by Codersarts