007-Django Views

Objectives: Django Views

Django Views

Django Views

A view in Django is a Python function that takes an HTTP request and returns an HTTP response, like an HTML page.

A web page typically consists of multiple views, each performing different tasks.

Where Views Are Located

Views are usually placed in a file called views.py located inside your app folder.

For example, if your app is named members, there will be a my_tennis_club/members/views.py file:

from django.shortcuts import render
# Create your views here.

Creating Your First View

Open views.py and replace the content with this simple example:

from django.shortcuts import render
from django.http import HttpResponse

def members(request):
    return HttpResponse("Hello world!")

Note: The name of the view function doesn’t have to match the app name. In this example, the function is called members because it fits the context.

Real-life Example:

Think of a view like a waiter in a restaurant. The customer (browser) makes a request (HTTP request), the waiter (view) goes to the kitchen (model or logic), and brings back a meal (HTTP response) to the customer.

Advice:

Start with simple views like this “Hello world!” before adding templates or database connections. It helps you understand how requests and responses work in Django.

Next Step: URLs

To display your view in a browser, you need to link it to a URL. We will cover URLs in the next chapter.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::