Skip to main content

Views

A view in Django is a Python function or class that takes a web request and returns a web response. The response can be the HTML contents of a webpage, or a redirect, or a 404 error, or an XML document, or an image... or anything, really.

Types of Views

In Django, there are two main types of views: class-based views (CBV) and function-based views (FBV). Both have their own advantages and disadvantages and can be used depending on the needs of your project.

Function-based Views (FBV):

  • FBVs are the traditional way of writing views in Django.
  • FBVs are written as Python functions, and take a request object as their first argument and return an HttpResponse object.
  • FBVs are simple to understand and easy to write for small and simple applications.
  • FBVs do not have any built-in methods, so all the logic and implementation have to be written from scratch.
  • FBVs can become complex and difficult to maintain as the application grows.

Class-based Views (CBV):

  • CBVs were introduced in Django version 1.3 and provide a more object-oriented way of writing views.
  • CBVs are written as classes and inherit from Django's base view classes.
  • CBVs provide built-in methods for common tasks such as rendering templates, handling forms, etc.
  • CBVs are reusable, making it easier to write generic views that can be reused across multiple applications.
  • CBVs can be more complex to understand compared to FBVs, but they are more flexible and can make it easier to manage the complexity of larger applications.

In conclusion, FBVs are best suited for small and simple applications, while CBVs are more suitable for complex and large applications. The choice between FBVs and CBVs should be based on the needs of your project, and the experience and comfort level of the developers working on it.

Creating Views

In order to create a view in Django, you will first need to create a views.py file in your app directory. Within this file, you will define your view functions.

For example, let's say you have a Manager model as defined in the previous chapter, you can create views for displaying a list of all managers, displaying a specific manager, creating a new manager, and updating a manager.

  1. Import the necessary modules and models.
from django.shortcuts import render
from .models import Manager, Club
  1. Create a function for displaying a list of all managers.
def manager_list(request):
managers = Manager.objects.all()
return render(request, 'manager_list.html', {'managers': managers})
  1. Create a function/class view for displaying a specific manager.
def manager_detail(request, pk):
manager = Manager.objects.get(pk=pk)
return render(request, 'manager_detail.html', {'manager': manager})
  1. Create a function for creating a new manager.
def manager_create(request):
if request.method == 'POST':
form = ManagerForm(request.POST)
if form.is_valid():
manager = form.save()
return redirect('manager_detail', pk=manager.pk)
else:
form = ManagerForm()
return render(request, 'manager_form.html', {'form': form})
  1. Create a function for updating a manager.
def manager_update(request, pk):
manager = Manager.objects.get(pk=pk)
if request.method == 'POST':
form = ManagerForm(request.POST, instance=manager)
if form.is_valid():
manager = form.save()
return redirect('manager_detail', pk=manager.pk)
else:
form = ManagerForm(instance=manager)
return render(request, 'manager_form.html', {'form': form})
  1. Add the view functions/classes to the URLs.
from django.urls import path
from .views import manager_list, manager_detail, manager_create, manager_update

urlpatterns = [
path('managers/', manager_list, name='manager_list'),
path('manager/<int:pk>/', manager_detail, name='manager_detail'),
path('manager/new/', manager_create, name='manager_create'),
path('manager/<int:pk>/edit/', manager_update, name='manager_update'),
]

The full code for the view

from django.shortcuts import render
from .models import Manager, Club


def manager_list(request):
managers = Manager.objects.all()
return render(request, 'manager_list.html', {'managers': managers})


def manager_detail(request, pk):
manager = Manager.objects.get(pk=pk)
return render(request, 'manager_detail.html', {'manager': manager})


def manager_create(request):
if request.method == 'POST':
form = ManagerForm(request.POST)
if form.is_valid():
manager = form.save()
return redirect('manager_detail', pk=manager.pk)
else:
form = ManagerForm()
return render(request, 'manager_form.html', {'form': form})


def manager_update(request, pk):
manager = Manager.objects.get(pk=pk)
if request.method == 'POST':
form = ManagerForm(request.POST, instance=manager)
if form.is_valid():
manager = form.save()
return redirect('manager_detail', pk=manager.pk)
else:
form = ManagerForm(instance=manager)
return render(request, 'manager_form.html', {'form': form})