Challenge - 5 Problems
Django Views Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Output of a function-based view with a redirect
What will be the HTTP status code returned by this Django function-based view when accessed?
Django
from django.http import HttpResponseRedirect def my_view(request): return HttpResponseRedirect('/home/')
Attempts:
2 left
💡 Hint
Think about what HttpResponseRedirect does in Django.
✗ Incorrect
HttpResponseRedirect returns a 302 status code to tell the browser to go to a new URL.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this class-based view
Which option correctly identifies the syntax error in this Django class-based view?
Django
from django.views import View from django.http import HttpResponse class MyView(View): def get(self, request): return HttpResponse('Hello') def post(self, request): return HttpResponse('Posted') def put(self, request): return HttpResponse('Put') def delete(self, request): return HttpResponse('Deleted') def patch(self, request): return HttpResponse('Patched') def options(self, request): return HttpResponse('Options') def trace(self, request): return HttpResponse('Trace') def head(self, request): return HttpResponse('Head') def connect(self, request): return HttpResponse('Connect') def get(self, request): return HttpResponse('Duplicate get')
Attempts:
2 left
💡 Hint
Look for repeated method names inside the class.
✗ Incorrect
Defining the 'get' method twice in the same class causes a syntax error because the second definition overwrites the first.
❓ state_output
advanced2:00remaining
State behavior difference between function-based and class-based views
Given these two views, what is the main difference in how they handle state between requests?
Django
from django.views import View from django.http import HttpResponse count = 0 def function_view(request): global count count += 1 return HttpResponse(f'Count is {count}') class ClassView(View): count = 0 def get(self, request): ClassView.count += 1 return HttpResponse(f'Count is {ClassView.count}')
Attempts:
2 left
💡 Hint
Consider where the count variable is stored and how Python handles globals and class variables.
✗ Incorrect
The function-based view uses a global variable to track count, while the class-based view uses a class variable. Both persist across requests but are stored differently.
🔧 Debug
advanced2:00remaining
Why does this class-based view raise an error?
This Django class-based view raises an error when accessed. What is the cause?
Django
from django.views import View from django.http import HttpResponse class MyView(View): def get(self): return HttpResponse('Hello')
Attempts:
2 left
💡 Hint
Check the method signature for class-based views.
✗ Incorrect
Class-based view methods like 'get' must accept 'self' and 'request' parameters. Missing 'request' causes a TypeError.
🧠 Conceptual
expert3:00remaining
Choosing between function-based and class-based views for complex logic
Which statement best explains why you might choose a class-based view over a function-based view in Django for a complex page?
Attempts:
2 left
💡 Hint
Think about code organization and reuse in object-oriented programming.
✗ Incorrect
Class-based views let you group related methods and use inheritance to share code, which helps manage complex logic better than function-based views.