DEV Community

Gopal Ghate
Gopal Ghate

Posted on

๐ŸŒ Django Views โ€“ Function-Based Views (FBVs) Explained

In Django, views are the heart of your application. They control what data is shown and how itโ€™s displayed. A view takes a request from the user and returns a response. In this article, weโ€™ll explore Function-Based Views (FBVs), how they work, and how to handle different request methods (GET, POST).

๐Ÿ— What is a View?

A view is a Python function (or class) that receives a web request and returns a web response.

Simplest view (blog/views.py):

from django.http import HttpResponse


def home(request):
    return HttpResponse("Hello, Django Views!")
Enter fullscreen mode Exit fullscreen mode

Then connect it in blog/urls.py:

from django.urls import path
from . import views


urlpatterns = [
    path('', views.home, name='home'),
]
Enter fullscreen mode Exit fullscreen mode

Visiting http://127.0.0.1:8000/ will now show: Hello, Django Views!

๐Ÿ“ฆ Rendering Templates in Views

Instead of plain text, we usually render HTML templates

def home(request):
    return render(request, 'blog/home.html', {"message": "Welcome to the Blog"})
Enter fullscreen mode Exit fullscreen mode

Now in blog/templates/blog/home.html

<h1>{{ message }}</h1>
Enter fullscreen mode Exit fullscreen mode

This will display: Welcome to the Blog

๐Ÿ”„ Handling GET and POST Requests

Views can handle multiple request methods:

def contact(request):
    if request.method == "POST":
        name = request.POST.get("name")
        return HttpResponse(f"Thanks for contacting us, {name}!")
    return HttpResponse("Contact Form Page")

Enter fullscreen mode Exit fullscreen mode
  • If itโ€™s a GET request, it shows the form page.

  • If itโ€™s a POST request, it processes submitted data.

๐Ÿ›  Returning Different Response Types

  • 1๏ธโƒฃ JSON Response
from django.http import JsonResponse


def api_data(request):
    return JsonResponse({"status": "success", "data": [1, 2, 3]})
Enter fullscreen mode Exit fullscreen mode
  • 2๏ธโƒฃ Redirect
from django.shortcuts import redirect


def redirect_home(request):
    return redirect('home')
Enter fullscreen mode Exit fullscreen mode

โšก Advantages of FBVs

  • Easy to understand and quick to write.

  • Perfect for simple views like forms, API endpoints, or basic pages.

  • Direct control over request/response handling.

โš ๏ธ When FBVs Become Complex

As logic grows, FBVs can become messy with many if/else conditions. Example:


def post_handler(request):
    if request.method == "GET":
        return HttpResponse("Show posts")
    elif request.method == "POST":
        return HttpResponse("Create post")
    elif request.method == "PUT":
        return HttpResponse("Update post")
    else:
        return HttpResponse("Unsupported method")
Enter fullscreen mode Exit fullscreen mode

This works or we can create a separate functions and urls, but it gets harder to maintain. Thatโ€™s why Django also provides Class-Based Views (CBVs) (coming in a later article).

๐Ÿ† Summary

  • Views take a request and return a response.

  • FBVs are functions that handle logic directly.

  • You can return HTML, JSON, or redirects.

  • FBVs are great for simple use cases, but can get messy for complex ones.

Top comments (0)