Introduction to Function-Based Views
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.
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.
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.
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. #}
- Forgetting that every view function must accept
requestas its first parameter. - Returning a plain Python string or None instead of an HttpResponse (or subclass) object.
- Doing heavy logic directly inside the view instead of delegating to models or helper functions, making views hard to test.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: