Skip to content
thesarfo

Note

Django Views

Writing a view function that takes a request and returns an HttpResponse.

views 0

An HTTP is a request response protocol. So every data exchange involves a request and a response. This is where the views.py file comes into play.

In your views.py file, you write views function. A views function takes a request and returns a response. It is a request handler.

So your views function takes a request as an argument, and returns an HttpResponse. Because of that you need to import the HttpResponse class from django.http. see below on how to write a simple django view.

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def say_hello(request):
return HttpResponse('Hello World')

Our first view has been created. Now we need to map the view to a URL. So that when we request that URL, the say_hello function will be called. Lets say when we make a request to “127.0.0.8000/playground/hello” our say_hello function will be called. How do we do that