Challenge - 5 Problems
View Base Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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!')
Attempts:
2 left
💡 Hint
Look at the get method and what it returns.
✗ Incorrect
The get method returns an HttpResponse with the text 'Hello, world!'. So the response content is exactly that string.
❓ lifecycle
intermediate2: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
Attempts:
2 left
💡 Hint
Think about how Django routes requests to methods.
✗ Incorrect
The dispatch method is called first to route the request to the correct handler like get or post.
📝 Syntax
advanced2: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')
Attempts:
2 left
💡 Hint
Python does not allow two methods with the same name in a class.
✗ Incorrect
The second get method overwrites the first. Django calls get with only request, but the method expects two arguments, causing TypeError.
❓ state_output
advanced2: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))
Attempts:
2 left
💡 Hint
Think about how Django creates view instances per request.
✗ Incorrect
Django creates a new instance of the view class for each request, so 'self.counter' starts at 0 each time and increments to 1.
🔧 Debug
expert2: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')
Attempts:
2 left
💡 Hint
Check which HTTP methods the view supports.
✗ Incorrect
Django's View class returns 405 if the HTTP method is not implemented as a method in the view class.