简体   繁体   English

如何在Django中使用urls.py中的变量重定向url模式?

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

I'd like to redirect url pattern with variables from urls.py . 我想 urls.py 变量重定向url模式。

I refer other stackoverflow solution , but I don't know when url having a variable like following code. 我引用其他stackoverflow解决方案 ,但我不知道什么时候url有一个变量,如下面的代码。

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/ . 使用此代码,django将重定向/permalink/1/foo//permalink/(?P<id>\\d+)/ ,而不是/permalink/1/

Is there any solution without using views.py ? 没有使用views.py解决方案?

Of course I know solution using controller, but I wonder is there any simpler solution with using url pattern. 当然我知道使用控制器的解决方案,但我想知道使用url模式是否有任何更简单的解决方案。

Passing url='/permalink/(?P<id>\\d+)/' to RedirectView will not work, because the view does not substitute the named arguments in the url. url='/permalink/(?P<id>\\d+)/'传递给RedirectView将不起作用,因为视图不会替换url中的命名参数。

However, RedirectView lets you provide the pattern_name instead of the url to redirect to. 但是, RedirectView允许您提供pattern_name而不是要重定向到的url The url is reversed using the same args and kwargs that were passed for the original view. 使用为原始视图传递的相同args和kwargs反转url。

This will work in your case, because both url patterns have one named argument, id . 这将适用于您的情况,因为两个url模式都有一个命名参数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 . 如果目标url模式使用其他参数,则不能使用urlpattern_name Instead, you can subclass RedirectView and override get_redirect_url . 相反,您可以get_redirect_url RedirectView并覆盖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. QuerystringRedirect放在视图模块中是一种好习惯。 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"),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM