RedirectView Class Helper
In the previous lesson, I showed you the four different ways of calling the
redirect() shortcut. In this lesson, I’m going to show you the
RedirectView class helper.
RedirectView is a convenience class that has a built-in view. A typical use for it is to call it directly from inside of an
I often use it this way to tell a browser that’s looking for the favicon in the root to go into the
static/ folder where I usually store it.
00:24 You can also overload this class in order to customize it for your needs.
First off, an example of using this directly inside of the
urls file. I’ll hit
/inline_indirect/ and pass in the parameter
'inline_redirect/' is defined with a parameter called
term. It’s a string. The
RedirectView class is directly used inside of the path as the view.
It is parameterized with a URL of where to go—in this case, Google—and the parameter can be altered using the
%s mechanism. The query string for Google includes
"%(term)s" portion will get filled in with the
term from the
When I call
camping, Google will get
And here’s that response.
curl comes back with a
302 and the
Location header is
camping filled in where
Looking at the same kind of example—but this time overloading the class—calling
/inherit_redirect/ using the
/inherit_redirect/ is also parameterized with a
SearchRedirectView inherits from the
RedirectView class. Inside of the
views file, you can see that class. In this case, all I’ve done is override the
.url attribute. The contents of the
.url attribute are exactly the same as directly calling it in the previous example—
"google.com", still using
"%(term)s"—and this will get populated as before.
Same idea: the response comes back, redirect to
google.com with the query string set to
02:27 In the third example, I’m going to override the entire method.
Calling the dev server, using
/random_redirect/, passing in the parameter
urls file maps to the
RandomSearchView, which inherits from
Inside of the
views file, lines 7 through 16 is the
RandomSearchView class. The meat of it is the overridden
This is the method in the class that results in the redirect string. By overriding it, you can take complete control over what it does. In this particular example, I’m randomly picking one of the URLs defined in
search_urls—lines 8 through 12—and when I’ve picked it, I parameterize it. Just like before, it supports the
"%(term)s" mechanism, passing in the
term parameter from the
The end result: Bing was randomly chosen and parameterized with
sailing. There are a number of things that you can override if you’re subclassing
You can specify the
.url attribute for a hardcoded string. Instead, you can set
.pattern_name to specify a named URL pattern. There’s a boolean called
.permanent which defaults to
False, but if set to
True uses a permanent redirect.
There’s another boolean called
.query_string. If this is
True, any query string that’s sent in with the original URL is appended to the redirect.
This also defaults to
.get_redirect_url() allows you to fine-tune your control. If your method returns
410 Gone status is sent back to the browser.
The default implementation of
.get_redirect_url() checks for the
.url attribute. If it doesn’t find it, then it checks for the
.pattern_name attribute. Both the
.pattern_name attributes support the
%s placeholders, passing in keyword arguments from the URL pattern. When should you use this class? Well, it’s an easy way of putting a redirect right inside of your
If you’re already using class-based views, overloading
RedirectView would be consistent with your code. I’m not particularly a big fan of class-based views.
I find function-based views easier to read, and I would rather have a one-line function view with a
redirect() shortcut call in it, than overload the class.
I tend to only use this myself if I’m directly overloading in the
urls file. But if your codebase uses class-based views or you find them easier to understand, that’s what it’s there for. Next up, I’ll show you how to use parameters inside of your redirects.
Become a Member to join the conversation.