Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Django redirect function do in a view?
easy
A. It saves data to the database before showing a page.
B. It sends the user to a different URL or view automatically.
C. It stops the server from responding to the request.
D. It renders a template without changing the URL.

Solution

  1. Step 1: Understand the purpose of redirect

    The redirect function is used to send users to another page automatically, changing the URL in the browser.
  2. Step 2: Compare with other options

    Rendering a template does not change the URL, stopping the server or saving data are unrelated to redirect.
  3. Final Answer:

    It sends the user to a different URL or view automatically. -> Option B
  4. Quick Check:

    Redirect = automatic URL change [OK]
Hint: Redirect changes URL and sends user to another page [OK]
Common Mistakes:
  • Confusing redirect with render which does not change URL
  • Thinking redirect saves data automatically
  • Assuming redirect stops server response
2. Which of the following is the correct way to use redirect to send a user to a URL path '/home/'?
easy
A. redirect('/home/')
B. return redirect url('/home/')
C. return redirect('/home/')
D. return redirect('/home')()

Solution

  1. Step 1: Check the syntax of redirect usage

    The redirect function must be returned from the view with the URL path as a string argument.
  2. Step 2: Identify correct syntax

    Only return redirect('/home/') correctly calls redirect and returns the response.
  3. Final Answer:

    return redirect('/home/') -> Option C
  4. Quick Check:

    Use return with redirect and URL string [OK]
Hint: Always use return with redirect and pass URL as string [OK]
Common Mistakes:
  • Forgetting to return the redirect response
  • Calling redirect without return
  • Adding extra parentheses or wrong syntax
3. What will happen when this Django view runs?
from django.shortcuts import redirect

def my_view(request):
    return redirect('home')

Assuming 'home' is a valid URL pattern name.
medium
A. The user will be redirected to the URL linked to the 'home' view name.
B. The view will raise an error because 'home' is not a URL path.
C. The view will render a template named 'home'.
D. Nothing happens; the redirect function does not work with names.

Solution

  1. Step 1: Understand redirect with view name

    Redirect can accept a URL pattern name and will find the URL linked to that name.
  2. Step 2: Confirm behavior

    Since 'home' is a valid URL name, redirect sends user to that URL automatically.
  3. Final Answer:

    The user will be redirected to the URL linked to the 'home' view name. -> Option A
  4. Quick Check:

    redirect('view_name') sends user to that URL [OK]
Hint: redirect accepts URL names, not just paths [OK]
Common Mistakes:
  • Thinking redirect only works with URL paths
  • Confusing redirect with render for templates
  • Assuming redirect needs full URL string
4. Identify the error in this Django view using redirect:
from django.shortcuts import redirect

def go_home(request):
    redirect('/home/')
medium
A. Missing return statement before redirect call.
B. Redirect function does not accept string paths.
C. The URL path should not start with a slash.
D. The import statement is incorrect.

Solution

  1. Step 1: Check the redirect usage in the view

    The redirect function must be returned to send the HTTP response properly.
  2. Step 2: Identify missing return

    Here, redirect is called but not returned, so the view returns None and does not redirect.
  3. Final Answer:

    Missing return statement before redirect call. -> Option A
  4. Quick Check:

    Always return redirect response [OK]
Hint: Always return redirect, don't just call it [OK]
Common Mistakes:
  • Calling redirect without return
  • Thinking redirect needs special URL format
  • Incorrect import statements
5. You want to redirect a user to a detail page of a model instance post in Django. Which is the correct way to use redirect?
hard
A. return redirect('/post/' + post.id)
B. return redirect('post_detail', id=post.id)
C. return redirect(post)
D. return redirect('post_detail', pk=post.id)

Solution

  1. Step 1: Understand redirect with model instance and URL parameters

    Redirect can accept a view name with keyword arguments matching URL parameters.
  2. Step 2: Analyze options

    return redirect(post) passes the model instance directly, which works only if the model has a get_absolute_url method. return redirect('post_detail', id=post.id) uses incorrect keyword 'id'; typical patterns use 'pk', causing NoReverseMatch. return redirect('post_detail', pk=post.id) correctly passes the view name and keyword argument 'pk' matching typical URL patterns. return redirect('/post/' + post.id) tries to concatenate string and int without conversion, causing error.
  3. Final Answer:

    return redirect('post_detail', pk=post.id) -> Option D
  4. Quick Check:

    Use redirect with view name and keyword args [OK]
Hint: Use redirect with view name and keyword args for model pages [OK]
Common Mistakes:
  • Passing model instance without get_absolute_url method
  • Using incorrect keyword arg name like 'id' instead of 'pk'
  • Concatenating string and int without conversion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard