Here are resources for more information on Django views and URL patterns:
Django's Redirect Shortcut
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
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
The default status code is
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.
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?
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.
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.
Here’s the non-redirected view. Using
curl, I’m going to hit the dev server, looking at the
/details/ URL parameterized with
This URL can be found in
'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.
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
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
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.
'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
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.
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
On to the next one. This one is
Once again, inside of the
'redirect_by_view/' is found parameterized by our product and associated with the view named
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.
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
The third example is using
product.urls, like before, this is parameterized with
product_id. This one is associated with a view named
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.
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.
Like before, the result comes back, this time looking at product
Propane. There’s a fourth way of calling
redirect(), and that’s by passing in an object that defines
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
This is the code behind the
Product object that implements this mechanism. Lines 11, 12, and 13 define the
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.
This is similar to the
redirect() shortcut passing in the same tag.
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
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
Become a Member to join the conversation.