Bird
Raised Fist0
Djangoframework~30 mins

Function-based views basics in Django - Mini Project: Build & Apply

Choose your learning style10 modes available
Function-based views basics
📖 Scenario: You are building a simple Django web app that shows a welcome message on the homepage.This app uses function-based views to handle web requests.
🎯 Goal: Create a Django function-based view that returns a simple HTTP response with the text "Welcome to my site!" when the homepage is accessed.
📋 What You'll Learn
Create a function-based view named home_view in views.py
The view must return an HttpResponse with the exact text "Welcome to my site!"
Add a URL pattern in urls.py that maps the root URL '' to home_view
Use only function-based views, no class-based views
💡 Why This Matters
🌍 Real World
Function-based views are the simplest way to handle web requests in Django apps, useful for small projects or simple pages.
💼 Career
Understanding function-based views is essential for Django developers to build and maintain web applications.
Progress0 / 4 steps
1
Create the view function
In views.py, import HttpResponse from django.http. Then create a function called home_view that takes a request parameter.
Django
Hint

Use def to define the function and import HttpResponse at the top.

2
Return a simple HTTP response
Inside the home_view function, return an HttpResponse with the text "Welcome to my site!".
Django
Hint

Use return HttpResponse("Welcome to my site!") inside the function.

3
Import the view in urls.py
In urls.py, import the home_view function from views.
Django
Hint

Use from .views import home_view to import the view.

4
Add URL pattern for the homepage
In urls.py, add a URL pattern that maps the root URL '' to the home_view function using path. Make sure to import path from django.urls.
Django
Hint

Use path('', home_view) inside urlpatterns list.

Practice

(1/5)
1. What is the main purpose of a function-based view in Django?
easy
A. To handle web requests and return responses
B. To define database models
C. To style HTML pages with CSS
D. To manage user sessions automatically

Solution

  1. Step 1: Understand the role of views in Django

    Views are responsible for processing web requests and returning responses.
  2. Step 2: Identify the function-based view role

    Function-based views are simple Python functions that take a request and return a response.
  3. Final Answer:

    To handle web requests and return responses -> Option A
  4. Quick Check:

    Function-based views handle requests = A [OK]
Hint: Views handle requests and responses in Django [OK]
Common Mistakes:
  • Confusing views with models
  • Thinking views handle styling
  • Assuming views manage sessions automatically
2. Which of the following is the correct way to define a simple function-based view in Django?
easy
A. def my_view(request): print('Hello')
B. class my_view: return HttpResponse('Hello')
C. def my_view(): return HttpResponse('Hello')
D. def my_view(request): return HttpResponse('Hello')

Solution

  1. Step 1: Check function signature for views

    A function-based view must accept a request parameter.
  2. Step 2: Verify the return value

    The view must return an HttpResponse object, not just print or omit request.
  3. Final Answer:

    def my_view(request): return HttpResponse('Hello') -> Option D
  4. Quick Check:

    Function with request param and HttpResponse return = A [OK]
Hint: View functions need request param and return HttpResponse [OK]
Common Mistakes:
  • Omitting the request parameter
  • Using class syntax instead of function
  • Printing instead of returning HttpResponse
3. Given this function-based view code, what will be the HTTP response content?
from django.http import HttpResponse

def greet(request):
    name = request.GET.get('name', 'Guest')
    return HttpResponse(f'Hello, {name}!')
medium
A. Hello, None!
B. Hello, Guest!
C. Hello, name!
D. Error: missing name parameter

Solution

  1. Step 1: Understand request.GET.get usage

    The code tries to get 'name' from query parameters; if missing, defaults to 'Guest'.
  2. Step 2: Determine response content when no name given

    If no 'name' parameter is in the URL, it uses 'Guest', so response is 'Hello, Guest!'.
  3. Final Answer:

    Hello, Guest! -> Option B
  4. Quick Check:

    Missing name defaults to Guest = B [OK]
Hint: GET.get with default returns default if param missing [OK]
Common Mistakes:
  • Assuming missing param causes error
  • Confusing string literal with variable name
  • Expecting None instead of default value
4. Identify the error in this function-based view code:
from django.http import HttpResponse

def my_view():
    return HttpResponse('Hi')
medium
A. Return statement syntax is incorrect
B. HttpResponse is not imported
C. Missing request parameter in function definition
D. Function name is invalid

Solution

  1. Step 1: Check function parameters for views

    Function-based views must accept a request parameter to receive HTTP requests.
  2. Step 2: Verify imports and syntax

    HttpResponse is imported correctly and return syntax is valid; function name is valid.
  3. Final Answer:

    Missing request parameter in function definition -> Option C
  4. Quick Check:

    View functions need request param = C [OK]
Hint: Always include request parameter in view functions [OK]
Common Mistakes:
  • Omitting request parameter
  • Forgetting to import HttpResponse
  • Misnaming the function
5. You want to create a function-based view that returns a 404 error if a query parameter 'id' is missing. Which code snippet correctly implements this?
hard
A. def view(request): if request.GET.get('id') is None: return HttpResponse('Not found', status=404) return HttpResponse('Found')
B. def view(request): if request.GET['id'] is None: return HttpResponse('Not found', status=404) return HttpResponse('Found')
C. def view(request): if request.GET.get('id') == '': return HttpResponse('Not found', status=404) return HttpResponse('Found')
D. def view(request): if 'id' not in request.POST: return HttpResponse('Not found', status=404) return HttpResponse('Found')

Solution

  1. Step 1: Check how to safely get query parameters

    Using request.GET.get('id') returns None if 'id' is missing, avoiding KeyError.
  2. Step 2: Verify condition to return 404

    If get returns None, return HttpResponse with status=404; else return 'Found'.
  3. Final Answer:

    def view(request): if request.GET.get('id') is None: return HttpResponse('Not found', status=404) return HttpResponse('Found') -> Option A
  4. Quick Check:

    Use GET.get to check missing param safely = D [OK]
Hint: Use request.GET.get to avoid KeyError when checking params [OK]
Common Mistakes:
  • Using request.GET['id'] without checking existence
  • Checking for empty string instead of None
  • Confusing request.GET with request.POST
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard