🌐 Django Views – Function-Based Views (FBVs) Explained (Article 4)


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


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

from django.http import JsonResponse


def api_data(request):
    return JsonResponse({"status": "success", "data": [1, 2, 3]})
Enter fullscreen mode

Exit fullscreen mode

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.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *