What are views in Django?

DWQA QuestionsCategory: QuestionsWhat are views in Django?
Editor Staff asked 4 weeks ago

What are views in Django?

1 Answers
Editor Staff answered 4 weeks ago

A view function, or “view” for short, is simply a Python function that takes a web request and returns a web response. This response can be HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image, etc. 
Example:

from django.http import HttpResponse
def sample_function(request):
 return HttpResponse(“Welcome to Django”)

There are two types of views:

  • Function-Based Views: In this, we import our view as a function.
  • Class-based Views: It’s an object-oriented approach.