17. Views in Django

Página 91

In Chapter 17 of our course on building systems with Python and Django, we'll cover a crucial component of Django - Views. Views are responsible for processing user requests and returning a response. In other words, they are the bridge between templates and templates.

Views in Django are Python functions that take a web request and return a response. That response could be HTML from a web page, a redirect, a 404 error, XML, an image, or anything else. The view itself doesn't contain any information about what should be sent to the user. For this, it relies on models and templates.

There are two types of Views in Django: function-based (FBVs) and class-based (CBVs). FBVs are simple and easy to use, especially for beginners. However, for larger and more complex projects, CBVs are generally a better choice as they offer more flexibility and functionality.

FBVs are defined as normal Python functions, which take a request object as a parameter and return a response object. For example:

def my_view(request):
    return HttpResponse('Hello World!')

CBVs, on the other hand, are defined as Python classes. They are more powerful and flexible than FBVs, as they can take advantage of Python's object orientation and DRY (Don't Repeat Yourself). CBVs also provide a consistent structure for your views, making your code easier to read and maintain. For example:

class MyView(View):
    def get(self, request):
        return HttpResponse('Hello World!')

To use a view, you need to map it to a URL. This is done in a file called urls.py. Each Django application can have its own urls.py file, and the main Django project also has one. URLs are mapped to views using regular expressions or simple string routes.

For example, to map the URL /my_view to the view my_view, you would add the following to your urls.py file:

urlpatterns = [
    path('my_view/', views.my_view, name='my_view'),
]

Views can also process user data. For example, if you want to process data from a form, you can do this in a view. The view takes the data from the form, validates it, and then saves the data to a database or performs other actions.

For example, here is a view that renders a contact form:

def contato_view(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('success')
    else:
        form = ContactForm()
    return render(request, 'contact.html', {'form': form})

In short, views are an essential part of Django. They process user requests, work with templates to get and save data, and use templates to create the response that will be sent to the user. Mastering views is a crucial step in becoming an effective Django developer.

Now answer the exercise about the content:

What is the main function of Views in Django?

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

You missed! Try again.

Next page of the Free Ebook:

9217.1. Views in Django: Introduction to Views in Django

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