Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Django's Redirect Shortcut

Here are resources for more information on Django views and URL patterns:

00:00 In the previous lesson, I introduced you to the concept of an HTTP redirect. In this lesson, I’m going to dive into Django’s redirect() shortcut, and how to use it. In the introduction, I quickly demonstrated the use of the Django redirect() shortcut.

00:15 Why is it a shortcut? Well, because you can do it without the shortcut. Here’s a handmade version of what it’s doing. First off, you need an HttpResponse object.

00:25 The default status code is 200, or OK. This needs to be changed to 302, for redirect. Secondly, you have to add a header to the response, which is the location of where you wish to redirect to. Finally, just return that.

00:41 Hitting this with curl

00:46 and you can see the 302 header and the Location. But why write that code over and over again, when the shortcut redirect() function can do it for you?

00:56 redirect() can be called with a variety of different arguments. You can call it with a URL like I’ve shown you. You can call it with a named URL path.

01:05 You can call it with a reference to a view. Or, you can pass it an object that supports the get_absolute_url() method. I’ll show you the first three in practice, and then I’ll come back to the object method, later.

01:18 In order to demonstrate redirecting, I need somewhere to redirect to. Throughout this example, I’m going to be using a Django app that returns details about a product.

01:27 Here’s the non-redirected view. Using curl, I’m going to hit the dev server, looking at the /details/ URL parameterized with 1.

01:38 This URL can be found in product.urls. The 'details/' URL is parameterized with a product_id, associated with a view named details, and tagged with the name 'product-details', for further use later.

01:55 Looking at the corresponding views file, line 7 looks up the product, and line 8 returns an HttpResponse object containing the product.name and using content_type="text/plain".

02:07 When this function returns, curl gets the response, printing Prod 1:Tent. Now, for our redirect() method. Once again using curl, this time hitting the URL /redirect_by_name/.

02:24 Notice that I’m using the -L parameter of curl. By default, curl does not follow redirects. -L tells it to follow the redirects and go to the result.

02:37 'redirect_by_name/' is found inside of product.urls. Just like 'details/', this is parameterized using a product_id, and this one is associated with redirect_by_name in the views.

02:51 The corresponding function in the views file uses the redirect() shortcut. This use of the shortcut is using the "product-details" name tag, associating with the URL having the name "product-details". It’s parameterized with the product_id that was passed in.

03:12 At the bottom, you can see the result: curl is redirected to /details/, passing in 2 as a parameter, and what comes back is the product Sleeping Bag.

03:24 On to the next one. This one is /redirect_by_view/.

03:33 Once again, inside of the product.urls file, 'redirect_by_view/' is found parameterized by our product and associated with the view named redirect_by_view.

03:45 The corresponding function in the views file is redirect_by_view(). This time, the shortcut is taking a function pointer. This is a function pointer to the actual details() view function.

03:57 This mode of redirect is the one I like best. You associate the redirected view with the view that you want to be redirected to. You don’t have to have any knowledge of what’s going on inside of the urls file. The returned result of this, with the parameter 3, gives you Prod 3:Lantern.

04:16 The third example is using /redirect_by_url/.

04:26 Back to product.urls, like before, this is parameterized with product_id. This one is associated with a view named redirect_by_url.

04:37 This view calls the redirect() shortcut function, passing in a hardcoded URL. It uses the % string mechanism in order to parameterize the URL. In the demo I gave in the introduction, this was the shortcut that I used. Generally, it’s considered bad practice, though.

04:54 It’s bad practice because if you need to change your URL, you now have to remember to change it inside of your view file as well as inside of the urls file. So it’s good for a quick demonstration but otherwise, you should avoid using this mechanism in production code.

05:09 Like before, the result comes back, this time looking at product 4, Propane. There’s a fourth way of calling redirect(), and that’s by passing in an object that defines get_absolute_url().

05:22 One of the advantages to this is the Django admin is aware of it. If you implement this method on an object model, the Django admin will automatically add a reference link. On the models page, a VIEW ON SITE button is added, redirecting the user to the result of get_absolute_url().

05:41 This is the code behind the Product object that implements this mechanism. Lines 11, 12, and 13 define the get_absolute_url() method.

05:50 All this method has to do is return a URL to redirect to. In this case, I’m using the reverse() method from django.urls in order to look up the named tag "product-details", and return that URL.

06:05 This is similar to the redirect() shortcut passing in the same tag.

06:12 Inside of the views file, you can see this in practice. Line 11 looks up the Product object and line 12 calls the redirect() shortcut, passing in the product.

06:22 redirect() looks up get_absolute_url() and returns its result. Next up, if you’re a fan of class-based views, Django also has a RedirectView class.

Become a Member to join the conversation.