0
0
Djangoframework~10 mins

Function-based vs class-based decision in Django - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function-based view that returns a simple HTTP response.

Django
from django.http import HttpResponse

def my_view(request):
    return [1]("Hello, world!")
Drag options to blanks, or click blank then click option'
Arender
BHttpResponse
Credirect
DJsonResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using render instead of HttpResponse for simple text response
Forgetting to return the response object
2fill in blank
medium

Complete the code to import the base class for class-based views.

Django
from django.views import [1]
Drag options to blanks, or click blank then click option'
ATemplateView
BHttpResponse
CView
DListView
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a specific view class instead of the base View
Confusing HttpResponse with a view class
3fill in blank
hard

Fix the error in the class-based view by completing the method name that handles GET requests.

Django
from django.views import View
from django.http import HttpResponse

class MyView(View):
    def [1](self, request):
        return HttpResponse("Hello from class-based view")
Drag options to blanks, or click blank then click option'
Adispatch
Bhandle
Cpost
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for GET requests
Using 'dispatch' directly instead of 'get'
4fill in blank
hard

Fill both blanks to complete a function-based view that renders a template with context.

Django
from django.shortcuts import [1]

def home(request):
    context = {'name': 'Alice'}
    return [2](request, 'home.html', context)
Drag options to blanks, or click blank then click option'
Arender
BHttpResponse
Credirect
Dget_object_or_404
Attempts:
3 left
💡 Hint
Common Mistakes
Importing render but returning HttpResponse directly
Using redirect instead of render for templates
5fill in blank
hard

Fill all three blanks to complete a class-based view that renders a template with extra context.

Django
from django.views.generic import [1]

class WelcomeView([2]):
    template_name = 'welcome.html'

    def get_context_data(self, **kwargs):
        context = super().[3](**kwargs)
        context['user'] = 'Bob'
        return context
Drag options to blanks, or click blank then click option'
ATemplateView
BView
Cget_context_data
Ddispatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using View instead of TemplateView for template rendering
Calling super().dispatch instead of super().get_context_data