0
0
Djangoframework~20 mins

Redirects with redirect function in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redirect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django view return?
Consider this Django view function using the redirect function. What will the user see after accessing this view?
Django
from django.shortcuts import redirect

def my_view(request):
    return redirect('home')
AThe view raises a NameError because 'home' is not defined.
BThe user sees a page with the text 'home'.
CThe user is redirected to the URL mapped to the name 'home'.
DThe user sees a 404 error page.
Attempts:
2 left
💡 Hint
Think about what the redirect function does with a string argument.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses the redirect function to send to a URL path?
You want to redirect the user to the URL path '/dashboard/'. Which code snippet correctly does this?
Areturn redirect('dashboard')
Breturn redirect(path='/dashboard/')
Creturn redirect(url='/dashboard/')
Dreturn redirect('/dashboard/')
Attempts:
2 left
💡 Hint
The redirect function can take a URL path as a string directly.
🔧 Debug
advanced
2:00remaining
Why does this redirect cause an error?
This Django view tries to redirect using a variable but causes an error. Why?
def my_view(request):
    url_name = home
    return redirect(url_name)
Django
def my_view(request):
    url_name = home
    return redirect(url_name)
ANameError because 'home' is not defined as a variable or string.
BTypeError because redirect expects a string but got a variable.
CNo error, it redirects correctly to 'home'.
DValueError because 'home' is not a valid URL.
Attempts:
2 left
💡 Hint
Look at how the variable url_name is assigned.
state_output
advanced
2:00remaining
What is the HTTP status code of this redirect response?
Given this Django view, what HTTP status code will the redirect response have?
from django.shortcuts import redirect

def my_view(request):
    return redirect('login')
Django
from django.shortcuts import redirect

def my_view(request):
    return redirect('login')
A302 Found (temporary redirect)
B301 Moved Permanently
C404 Not Found
D200 OK
Attempts:
2 left
💡 Hint
By default, redirect sends a temporary redirect status.
🧠 Conceptual
expert
3:00remaining
Which redirect usage correctly passes URL parameters?
You want to redirect to a URL named 'profile' that requires a username parameter. Which option correctly passes the parameter using Django's redirect function?
Areturn redirect('profile', {'username': 'alice'})
Breturn redirect('profile', username='alice')
Creturn redirect('profile?username=alice')
Dreturn redirect('profile/username/alice')
Attempts:
2 left
💡 Hint
Use keyword arguments to pass URL parameters to redirect.