How to redirect url pattern with variables from urls.py in Django?

混江龙づ霸主 提交于 2019-11-30 03:42:47

问题


I'd like to redirect url pattern with variables from urls.py.

I refer other stackoverflow solution, but I don't know when url having a variable like following code.

from django.conf.urls import patterns, url
from django.views.generic import RedirectView


urlpatterns = patterns(
    url(
        r'^permalink/(?P<id>\d+)/foo/$',
        RedirectView.as_view(url='/permalink/(?P<id>\d+)/')
    ),
)

With this code, django will redirect /permalink/1/foo/ to /permalink/(?P<id>\d+)/, not the /permalink/1/.

Is there any solution without using views.py?

Of course I know solution using controller, but I wonder is there any simpler solution with using url pattern.


回答1:


Passing url='/permalink/(?P<id>\d+)/' to RedirectView will not work, because the view does not substitute the named arguments in the url.

However, RedirectView lets you provide the pattern_name instead of the url to redirect to. The url is reversed using the same args and kwargs that were passed for the original view.

This will work in your case, because both url patterns have one named argument, id.

urlpatterns = [
    url(r'^permalink/(?P<id>\d+)/foo/$',
        RedirectView.as_view(pattern_name="target_view"),
        name="original_view"),
    url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]

If the target url pattern uses other arguments, then you can't use url or pattern_name. Instead, you can subclass RedirectView and override get_redirect_url.

from django.core.urlresolvers import reverse
from django.views.generic import RedirectView

class QuerystringRedirect(RedirectView):
    """
    Used to redirect to remove GET parameters from url

    e.g. /permalink/?id=10 to /permalink/10/   
    """

    def get_redirect_url(self):
        if 'id' in self.request.GET:
            return reverse('target_view', args=(self.request.GET['id'],))
        else:
            raise Http404()

It would be good practice to put QuerystringRedirect in your views module. You would then add the view to your url patterns with something like:

urlpatterns = [
    url(r'^permalink/$', views.QuerystringRedirect.as_view(), name="original_view"),
    url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]


来源:https://stackoverflow.com/questions/31661044/how-to-redirect-url-pattern-with-variables-from-urls-py-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!