17.1. Views in Django: Introduction to Views in Django

Página 92

17.1. Views in Django: Introduction to Views in Django

Django, a powerful web framework written in Python, is famous for its ability to speed up the web application development process. One of the main features of Django is its MVT (Model-View-Template) architecture, which is a variation of the MVC (Model-View-Controller) design pattern. In this context, views play a crucial role in handling and presenting data to the user.

What are Views in Django?

Views, in Django, are Python functions that take a web request and return a response. That response could be the HTML of a web page, a redirect, a 404 error, an XML document, an image, or anything that can be displayed through a web browser. Views in Django are the bridge between models (the data access layer) and templates (the presentation layer).

Creating a View

To create a view in Django, you first need to create a function in Python. This function must accept at least one argument: an HttpRequest object. Additionally, the function must return an HttpResponse object. See a simple example of a view:


from django.http import HttpResponse

def hello_world(request):
    return HttpResponse("Hello, World!")

In this example, the hello_world() function is a view that accepts a web request and returns a response containing the string "Hello, World!".

Configuring URLs for the View

After creating the view, you need to configure a URL for it. This is done in your Django application's urls.py file. You need to import the view you created and then add a new entry to the urlpatterns list, which maps URLs to views. See an example:


from django.urls import path
from .views import hello_world

urlpatterns = [
    path('hello/', hello_world, name='hello_world'),
]

In this example, the URL /hello/ is mapped to the hello_world() view. When a user accesses that URL, Django calls the hello_world() function and displays the result on the web page.

Class based views

Django also supports class-based views, which are an alternative to function-based views. Class-based views are especially useful when your view is performing standard operations that are common in most web applications, such as displaying a list of objects, displaying the details of a single object, creating a new object, etc.

To create a class-based view, you need to create a class that inherits from one of Django's view base classes and then override the appropriate methods. Here's an example of a class-based view that displays a list of objects:


from django.views.generic import ListView
from .models import MyModel

class MyModelListView(ListView):
    model = MyModel

In this example, the MyModelListView class is a view that displays a list of MyModel objects. The ListView class already contains the logic to retrieve the list of objects from the database and pass it to the template, so you don't have to write this logic yourself.

In short, views in Django are an essential part of web application development. They allow you to control how data is presented to the user and how user requests are processed. Whether using function-based views or class-based views, Django offers a flexible and powerful way to create rich, interactive web applications.

Now answer the exercise about the content:

What are views in Django and what is their function?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

9317.2. Views in Django: Creating a simple view

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text