0
0
Djangoframework~10 mins

Why class-based views exist in Django - Test Your Understanding

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

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

Django
from django.views import [1]
Drag options to blanks, or click blank then click option'
AView
BModel
CTemplate
DForm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Model' which is for database models, not views.
Using 'Template' or 'Form' which are unrelated to base view classes.
2fill in blank
medium

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

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

class HelloView(View):
    def [1](self, request):
        return HttpResponse('Hello!')
Drag options to blanks, or click blank then click option'
Arender
Bpost
Cinit
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' when the view is meant to handle GET requests.
Using 'init' which is a constructor, not a request handler.
3fill in blank
hard

Fix the error in the class-based view method signature to correctly accept the request and return a response.

Django
class MyView(View):
    def get([1]):
        return HttpResponse('Hi')
Drag options to blanks, or click blank then click option'
Arequest
Brequest, self
Cself, request
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting self parameter.
Placing request before self.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Django
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B<
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Mapping word to word itself instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if value is positive.

Django
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upper()' for keys.
Using '<' instead of '>' in the condition.
Swapping keys and values in the comprehension.