Practical examples of Django view function examples for beginners

If you’re just starting with Django, nothing beats seeing real code in action. This guide walks through practical examples of Django view function examples for beginners, written in plain English and kept as simple as possible. Instead of only talking theory, we’ll build small views you can drop straight into a `views.py` file and run today. You’ll see an example of returning plain text, rendering templates, handling forms, processing JSON, and even a tiny API-style view. These examples of Django view function patterns are the same building blocks used in real apps in 2024 and 2025, just stripped down so you can actually understand what’s going on. By the end, you won’t just copy and paste; you’ll know why each view works and how to tweak it for your own project. Grab a fresh Django project, open `views.py`, and let’s write some views together.
Written by
Taylor
Published

Simple examples of Django view function examples for beginners

Let’s start exactly where beginners struggle: “What does a Django view function actually look like?” No theory, just code.

Here is the smallest useful example of a Django view function you can write:

## views.py
from django.http import HttpResponse

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

Hook it up in urls.py:

## urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("hello/", views.hello_world, name="hello_world"),
]

Visit /hello/ in your browser and you’ll see plain text. This is one of the best examples of Django view function examples for beginners, because it shows the whole flow:

  • A view is just a Python function.
  • It takes a request object.
  • It returns an HttpResponse.

Once that clicks, every other example of a view is just a variation on this pattern.


Rendering HTML templates: examples include dynamic greetings

Plain text is nice, but you probably want HTML. The next step in our examples of Django view function examples for beginners is rendering a template.

## views.py
from django.shortcuts import render

def home(request):
    context = {"title": "My First Django Page", "username": "Taylor"}
    return render(request, "home.html", context)

Template home.html:

<!DOCTYPE html>
<html>
  <head>
    <title>{{ title }}</title>
  </head>
  <body>
    <h1>Welcome, {{ username }}!</h1>
    <p>This page is powered by a Django view function.</p>
  </body>
</html>

This example of a template-rendering view shows how data flows from Python into HTML. In real projects, examples include dashboards, profile pages, and product listings that all start with this exact pattern: render(request, template_name, context).

If you want to go deeper into templates and context, the official Django documentation is still the best reference: https://docs.djangoproject.com/en/stable/topics/http/views/.


Modern apps rarely show the same page to everyone. You often read data from the URL, like ?q=django. Here’s one of the most common real examples of Django view function examples for beginners: a tiny search view.

## views.py
from django.http import HttpResponse

def search(request):
    query = request.GET.get("q", "")  # default to empty string

    if query:
        message = f"You searched for: {query}"
    else:
        message = "You didn't type a search term."

    return HttpResponse(message)

URL config:

## urls.py
urlpatterns = [
    path("search/", search, name="search"),
]

Try /search/?q=django views in your browser. This pattern shows up in real examples like filtering products, searching blog posts, or sorting tables. You read from request.GET, do some logic, and send back a response.


Form handling examples of Django view function patterns

Every beginner eventually hits forms and thinks, “Okay, now it’s getting serious.” Let’s keep it friendly.

Here’s a classic example of a view function that handles a simple feedback form without Django’s form classes, so you can see the raw request handling.

## views.py
from django.shortcuts import render
from django.http import HttpResponse


def feedback(request):
    if request.method == "POST":
        name = request.POST.get("name", "Anonymous")
        message = request.POST.get("message", "")

#        # In a real app you might save to the database here

        return HttpResponse(f"Thanks for your feedback, {name}!")

#    # GET request: show the form
    return render(request, "feedback.html")

Template feedback.html:

<!DOCTYPE html>
<html>
  <body>
    <h1>Feedback form</h1>
    <form method="post">
      {% csrf_token %}
      <label>Your name:</label>
      <input type="text" name="name">

      <label>Your message:</label>
      <textarea name="message"></textarea>

      <button type="submit">Send</button>
    </form>
  </body>
</html>

This is one of the best examples of Django view function examples for beginners because it shows three important ideas in a single, short view:

  • Distinguishing between GET and POST.
  • Reading form data from request.POST.
  • Returning a different response after submission.

For a more structured approach using Django’s Form classes, the official tutorial is worth reading: https://docs.djangoproject.com/en/stable/intro/tutorial04/.


Returning JSON: examples include tiny API-style views

In 2024 and 2025, even beginner Django projects often talk to JavaScript frontends or mobile apps. That means JSON. Here’s a straightforward example of a JSON view function:

## views.py
from django.http import JsonResponse


def api_status(request):
    data = {
        "status": "ok",
        "framework": "Django",
        "year": 2025,
    }
    return JsonResponse(data)

This kind of view shows up in real examples like health checks, small internal tools, or prototype APIs before you move to Django REST Framework. It’s still just a regular view function; only the response type changes.

You can even accept JSON input:

import json
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, HttpResponseBadRequest


@csrf_exempt  # for demo only; be careful with this in production
def echo_json(request):
    if request.method != "POST":
        return HttpResponseBadRequest("POST required")

    try:
        payload = json.loads(request.body)
    except json.JSONDecodeError:
        return HttpResponseBadRequest("Invalid JSON")

    return JsonResponse({"you_sent": payload})

This gives you a real example of how Django view functions can power simple APIs without any extra libraries.


Using URL parameters: best examples with dynamic pages

Another pattern that beginners see in real projects is capturing values from the URL itself. Think /blog/2025/ or /users/42/. Here’s one of the clearest examples of Django view function examples for beginners using path parameters.

## urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("blog/<int:year>/", views.blog_by_year, name="blog_by_year"),
]
## views.py
from django.http import HttpResponse


def blog_by_year(request, year):
    return HttpResponse(f"Showing blog posts from {year}.")

Django passes year from the URL into your view function as a regular argument. Real examples include:

  • User profiles: /users/<int:id>/
  • Category pages: /category/<slug:slug>/
  • Order details: /orders/<uuid:order_id>/

Once you’re comfortable with these, you can build very readable URLs that match how people actually browse the web.


Redirects and post-submit patterns: examples include login flows

Another pattern that shows up again and again: do something, then redirect. Instead of returning HTML right after a form submission, you often redirect to another page.

Here’s an example of a view that simulates a login and then redirects:

## views.py
from django.shortcuts import redirect
from django.http import HttpResponse


def fake_login(request):
    if request.method == "POST":
#        # pretend we checked the username and password
        return redirect("dashboard")

    return HttpResponse("Show login form here")


def dashboard(request):
    return HttpResponse("Welcome to your dashboard!")

URL config:

## urls.py
urlpatterns = [
    path("login/", fake_login, name="login"),
    path("dashboard/", dashboard, name="dashboard"),
]

This small pattern mirrors real examples in production apps: after signup, after password reset, after profile update, and more. You do your work, then send the user somewhere more useful.

For security topics like authentication and sessions, using trusted references is always smart. The Django auth docs are a good starting point: https://docs.djangoproject.com/en/stable/topics/auth/default/.


Class-based vs function-based: why start with function examples?

By 2025, many tutorials jump straight to class-based views. They’re powerful, but for beginners, examples of Django view function examples for beginners are still the best way to understand what’s going on under the hood.

Function-based views make it obvious that:

  • A view is just “input → logic → output.”
  • You can read data from request.GET, request.POST, and request.body.
  • You choose how to respond: HttpResponse, JsonResponse, redirect, or render.

Once you’re comfortable with the examples include above, class-based views feel less magical because you can see they’re mostly shortcuts for patterns you already know.

If you’re interested in how people learn programming effectively, the teaching approaches used at universities like Harvard’s CS50 course (https://cs50.harvard.edu/) line up nicely with this: start with simple, clear examples, then layer on abstractions.


Putting it all together: a tiny 2025-ready mini app

Let’s stitch several of these examples of Django view function examples for beginners into a tiny, realistic mini app. Imagine a minimal “notes” app where you can:

  • See a homepage.
  • Create a note with a form.
  • List all notes as JSON.

This is still beginner-friendly but feels like something you might actually build.

## views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse

## Fake in-memory "database" for demo purposes only
NOTES = []


def notes_home(request):
    return render(request, "notes_home.html", {"notes": NOTES})


def add_note(request):
    if request.method == "POST":
        text = request.POST.get("text", "").strip()
        if text:
            NOTES.append(text)
        return redirect("notes_home")

    return render(request, "add_note.html")


def notes_api(request):
    return JsonResponse({"notes": NOTES})

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.notes_home, name="notes_home"),
    path("add/", views.add_note, name="add_note"),
    path("api/notes/", views.notes_api, name="notes_api"),
]

Templates:

notes_home.html:

<!DOCTYPE html>
<html>
  <body>
    <h1>Your notes</h1>
    <ul>
      {% for note in notes %}
        <li>{{ note }}</li>
      {% empty %}
        <li>No notes yet.</li>
      {% endfor %}
    </ul>
    <a href="{% url 'add_note' %}">Add a new note</a>
  </body>
</html>

add_note.html:

<!DOCTYPE html>
<html>
  <body>
    <h1>Add a note</h1>
    <form method="post">
      {% csrf_token %}
      <textarea name="text"></textarea>
      <button type="submit">Save</button>
    </form>
  </body>
</html>

Now you have:

  • A template-rendering view (notes_home).
  • A form-handling and redirecting view (add_note).
  • A JSON API view (notes_api).

These are real examples you can run locally and modify. They also show how the earlier small examples of Django view function examples for beginners combine into something that feels like an app.


FAQ about examples of Django view function patterns

What are some simple examples of Django view function usage?

Some of the simplest examples include:

  • A “Hello, world” view returning plain text with HttpResponse.
  • A view that renders a template with render() and passes a context dictionary.
  • A view that reads a q parameter from request.GET and shows a search result message.
  • A JSON view using JsonResponse to return a small dictionary.

All of these are short, real examples that beginners can understand in a few minutes.

Can you show an example of a Django view function that handles both GET and POST?

Yes. The feedback form earlier is a classic example of a Django view function that behaves differently depending on the HTTP method:

def feedback(request):
    if request.method == "POST":
#        # process form
        ...
        return HttpResponse("Thanks!")

#    # For GET, show the empty form
    return render(request, "feedback.html")

This pattern is one of the best examples for beginners because it mirrors how most real forms work.

Are function-based views still relevant in 2025, or should I skip to class-based views?

Function-based views are absolutely still relevant. Many tutorials and codebases use them, and they’re easier for beginners to read. Once you understand the examples of Django view function examples for beginners in this guide, you’ll be better prepared to understand what class-based views are doing behind the scenes.

Where can I find more real examples of Django view functions?

The official Django documentation has many code samples and is updated regularly: https://docs.djangoproject.com/. University courses that teach web development, such as those listed on https://www.ed.gov/ and open courses from places like Harvard (https://online-learning.harvard.edu/) or MIT (https://ocw.mit.edu/), often include Django or similar frameworks with practical examples.


By walking through these real, runnable examples of Django view function examples for beginners, you’ve seen how simple functions can power HTML pages, forms, redirects, and JSON APIs. From here, you can start layering in models, authentication, and class-based views, knowing you already understand the foundation: a view is just Python code that turns a request into a response.

Explore More Django Code Snippets

Discover more examples and insights in this category.

View All Django Code Snippets