Skip to content

Instantly share code, notes, and snippets.

@uolot
Last active June 19, 2016 17:25
Show Gist options
  • Save uolot/4171107 to your computer and use it in GitHub Desktop.
Save uolot/4171107 to your computer and use it in GitHub Desktop.
Django named url RedirectView
# views.py
from django.views.generic import RedirectView
from django.core.urlresolvers import reverse
class NamedUrlRedirectView(RedirectView):
def __init__(self, url, *args, **kwargs):
self.url = reverse(url)
super(NamedUrlRedirectView, self).__init__(*args, **kwargs)
# in urls.py
(r'^blog/category/pictures/$', NamedUrlRedirectView().as_view(url='gallery-index'))
@eugena
Copy link

eugena commented Dec 15, 2015

How about:

from django.views.generic import RedirectView

(r'^blog/category/pictures/$', RedirectView().as_view(pattern_name='gallery-index'))

?

@uolot
Copy link
Author

uolot commented Jun 19, 2016

@eugena: It was added in Django 1.6, while when the gist was create, Django's current version was 1.4. Nowadays it's definitely the correct solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment