class django.views.generic.base.RedirectView
Redirects to a given URL.
The given URL may contain dictionary-style string formatting, which will be interpolated against the parameters captured in the URL. Because keyword interpolation is always done (even if no arguments are passed in), any "%"
characters in the URL must be written as "%%"
so that Python will convert them to a single percent sign on output.
If the given URL is None
, Django will return an HttpResponseGone
(410).
Ancestors (MRO)
This view inherits methods and attributes from the following view:
django.views.generic.base.View
Method Flowchart
dispatch()
http_method_not_allowed()
get_redirect_url()
Example views.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from django.shortcuts import get_object_or_404 from django.views.generic.base import RedirectView from articles.models import Article class ArticleCounterRedirectView(RedirectView): permanent = False query_string = True pattern_name = 'article-detail' def get_redirect_url( self , * args, * * kwargs): article = get_object_or_404(Article, pk = kwargs[ 'pk' ]) article.update_counter() return super (ArticleCounterRedirectView, self ).get_redirect_url( * args, * * kwargs) |
Example urls.py:
1 2 3 4 5 6 7 8 9 10 | from django.conf.urls import url from django.views.generic.base import RedirectView from article.views import ArticleCounterRedirectView, ArticleDetail urlpatterns = [ url(r '^counter/(?P<pk>[0-9]+)/$' , ArticleCounterRedirectView.as_view(), name = 'article-counter' ), url(r '^details/(?P<pk>[0-9]+)/$' , ArticleDetail.as_view(), name = 'article-detail' ), url(r '^go-to-django/$' , RedirectView.as_view(url = 'https://djangoproject.com' ), name = 'go-to-django' ), ] |
Attributes
-
url
-
The URL to redirect to, as a string. Or
None
to raise a 410 (Gone) HTTP error.
-
pattern_name
-
The name of the URL pattern to redirect to. Reversing will be done using the same args and kwargs as are passed in for this view.
-
permanent
-
Whether the redirect should be permanent. The only difference here is the HTTP status code returned. If
True
, then the redirect will use status code 301. IfFalse
, then the redirect will use status code 302. By default,permanent
isFalse
.Changed in Django 1.9:The default value of the
permanent
attribute changed fromTrue
toFalse
.
-
query_string
-
Whether to pass along the GET query string to the new location. If
True
, then the query string is appended to the URL. IfFalse
, then the query string is discarded. By default,query_string
isFalse
.
Methods
-
get_redirect_url(*args, **kwargs)
-
Constructs the target URL for redirection.
The default implementation uses
url
as a starting string and performs expansion of%
named parameters in that string using the named groups captured in the URL.If
url
is not set,get_redirect_url()
tries to reverse thepattern_name
using what was captured in the URL (both named and unnamed groups are used).If requested by
query_string
, it will also append the query string to the generated URL. Subclasses may implement any behavior they wish, as long as the method returns a redirect-ready URL string.
Please login to continue.