0
0
Djangoframework~20 mins

View base class in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
View Base Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view?
Consider this Django view class. What will be the HTTP response content when a GET request is made?
Django
from django.http import HttpResponse
from django.views import View

class HelloView(View):
    def get(self, request):
        return HttpResponse('Hello, world!')
AThe response content will be empty
BThe response will be a 500 Internal Server Error
CThe response will be a 404 Not Found error
DThe response content will be 'Hello, world!'
Attempts:
2 left
💡 Hint
Look at the get method and what it returns.
lifecycle
intermediate
2:00remaining
Which method is called first when a request hits a Django View class?
Given a Django View class, which method is called first when processing any HTTP request?
Django
from django.views import View

class SampleView(View):
    def dispatch(self, request, *args, **kwargs):
        # some code
        pass
    def get(self, request):
        # some code
        pass
Aas_view
Bdispatch
C__init__
Dget
Attempts:
2 left
💡 Hint
Think about how Django routes requests to methods.
📝 Syntax
advanced
2:00remaining
What error does this Django View code raise?
Analyze this Django View code. What error will it raise when a GET request is made?
Django
from django.views import View
from django.http import HttpResponse

class BrokenView(View):
    def get(self, request):
        return HttpResponse('OK')
    def get(self, request, extra):
        return HttpResponse('Extra OK')
ATypeError: get() missing 1 required positional argument: 'extra'
BSyntaxError: duplicate method definition
CNo error, returns 'Extra OK'
DAttributeError: 'BrokenView' object has no attribute 'get'
Attempts:
2 left
💡 Hint
Python does not allow two methods with the same name in a class.
state_output
advanced
2:00remaining
What is the value of 'self.counter' after two GET requests?
Given this Django View class, what is the value of 'self.counter' after handling two separate GET requests?
Django
from django.views import View
from django.http import HttpResponse

class CounterView(View):
    counter = 0

    def get(self, request):
        self.counter += 1
        return HttpResponse(str(self.counter))
A1
B2
C0
DRaises AttributeError
Attempts:
2 left
💡 Hint
Think about how Django creates view instances per request.
🔧 Debug
expert
2:00remaining
Why does this Django View raise a 405 Method Not Allowed error?
This Django View raises a 405 error on POST requests. Why?
Django
from django.views import View
from django.http import HttpResponse

class MyView(View):
    def get(self, request):
        return HttpResponse('GET OK')
ABecause HttpResponse is not imported
BBecause the get() method raises an exception on POST
CBecause there is no post() method defined to handle POST requests
DBecause the dispatch method is missing
Attempts:
2 left
💡 Hint
Check which HTTP methods the view supports.