python - Django 1.5 url redirect with regex? -
i'm trying this:
(r'^$', redirectview.as_view(url='^(?p<username>\w+)/$')),
but doesn't seem parse regex part actual username...
i've done research can find examples redirect exact urls or other regex examples work in django 1.1
anyone have idea how in django 1.5+?
subclass redirectview
, , override get_redirect_view
method.
from django.core.urlresolvers import reverse class userredirectview(redirectview): permanent = false def get_redirect_url(self, pk): # better use reverse here return '/myapp/%s/' % self.request.user.username
you include userredirectview
in myapp.urls.py
module follows:
url(r'^$', userredirectview.as_view(), name='myapp_index'),
it better reverse
url instead of hardcoding /myapp/
in url above. example, if redirecting url pattern following
url(r'^(?p<username>\w+)/$', 'myapp.view_user', name='myapp_view_user'),
then change get_redirect_url
view to:
def get_redirect_url(self, pk): return reverse('myapp_view_user', args=(self.request.user.username,))
Comments
Post a Comment