Use Django RedirectView with a named url dynamic -
as can following:
my url config url(r'^video/show/(?p<slug>[-\w]+).html$', videodetail.as_view(), name="video_detail"), to: url(r'^video/(?p<slug>[-\w]+)$', video.as_view(), name="video_detail"),
the first current url , want use redirect second url config.
class videoredirectview(redirectview): permanent = true query_string = true def get_redirect_url(self, slug): video = get_object_or_404(video, slug__exact=self.kwargs['slug']) video.update_counter() return reverse('video_detail', args=(slug,))
as can pass on configurations of urls. thanks.
hi had similar problem whereby wanted dynamically redirect, whilst maintaining kwargs.
before had url like: /league/competition-name/video , wanted requests redirected /league/competition-name/all-video
within urls.py
url( r'^league/(?p<competition>[0-9a-za-z-]+)/(?p<tab>all-video|latest-news)/$', views.tabbedview.as_view(), name='competition' ), url( r'^league/(?p<competition>[0-9a-za-z-]+)/(?p<tab>video|latest)/$', views.tabbedpageredirectview.as_view(), name='competition-redirect' ),
then within views.py
from django.views.generic.base import redirectview class tabbedpageredirectview(redirectview): permanent = true query_string = true def get_redirect_url(self, *args, **kwargs): if kwargs['tab'] == 'latest': kwargs['tab'] = 'latest-news' if kwargs['tab'] == 'video': kwargs['tab'] = 'all-video' path = self.request.path path = path.replace('latest', 'latest-news') path = path.replace('video', 'all-video') self.url = path return super(tabbedpageredirectview, self).get_redirect_url(*args, **kwargs)
the trick build self url self.request.path. didn't find way of doing within urls.py file
Comments
Post a Comment