← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 8 of 10

Introduction to Function-Based Views

A function-based view is just a normal Python function that Django calls whenever someone visits a certain page, and its job is to send back a reply.

Writing a Basic View

A function-based view is a regular Python function defined in views.py. It must accept the HttpRequest object as its first argument (conventionally named request) and must return an HttpResponse object.

Example: Writing a Basic View

A function-based view is a regular Python function defined in views.py. It must accept the HttpRequest object as its first argument (conventionally named request) and must return an HttpResponse object.

markup
from django.http import HttpResponse

def home(request):
    return HttpResponse('Welcome to my Django site!')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Connecting the View to a URL

A view function does nothing on its own until a URL pattern points to it. Import the view module in urls.py and reference the function in a path() call.

Example: Connecting the View to a URL

A view function does nothing on its own until a URL pattern points to it. Import the view module in urls.py and reference the function in a path() call.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Views Can Branch on Logic

Because a view is just a function, it can contain regular Python control flow to decide what response to send back, such as checking the request method or query parameters.

Example: Views Can Branch on Logic

Because a view is just a function, it can contain regular Python control flow to decide what response to send back, such as checking the request method or query parameters.

markup
from django.http import HttpResponse

def greet(request):
    name = request.GET.get('name', 'stranger')
    return HttpResponse(f'Hello, {name}!')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting that every view function must accept request as its first parameter.
  2. Returning a plain Python string or None instead of an HttpResponse (or subclass) object.
  3. Doing heavy logic directly inside the view instead of delegating to models or helper functions, making views hard to test.
Chapter Summary
  • A function-based view (FBV) is a plain Python function that takes request and returns an HttpResponse.
  • Views are connected to URLs via path() entries in urls.py.
  • FBVs are simple and explicit, which makes them a great starting point before learning class-based views.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.