如何使用Django中urls.py的变量重定向url模式? [英] How to redirect url pattern with variables from urls.py in Django?

查看:159
本文介绍了如何使用Django中urls.py的变量重定向url模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用变量从 urls.py 重定向网址格式



我参考其他stackoverflow解决方案a>,但是我不知道url是否具有如下代码的变量。

 从django.conf.urls导入模式,url 
from django.views.generic import RedirectView


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

使用此代码,django将重定向 / permalink / 1 / foo / code>到 / permalink /(?P< id> \d +)/ ,而不是 / permalink / 1 / c $ c>。



有没有使用 views.py 的解决方案?



当然,我知道使用控制的解决方案呃,但是我想知道有没有使用url模式的简单解决方案。

解决方案

传递 url =' / permalink /(?P< id> \d +)/' RedirectView 将不起作用,因为该视图不会替换命名参数



但是, RedirectView 可让您提供 pattern_name ,而不是 url 重定向到。使用与原始视图相同的args和kwarg来反转url。



这将适用于您的情况,因为两个url模式都有一个命名参数, id

  urlpatterns = [
url(r'固定链接/(?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),
]

如果目标网址格式使用其他参数,则不能使用 url pattern_name 。相反,您可以将 RedirectView 子类化,并覆盖 get_redirect_url



来自django.core.urlresolvers import逆向
从django.views.generic导入RedirectView

class QuerystringRedirect(RedirectView):

用于重定向以从url中删除GET参数

eg / permalink /?id = 10 to / permalink / 10 /


def get_redirect_url(self):
如果self.request.GET中的'id':
return reverse('target_view',args =(self.request.GET ['id'],) )
else:
raise Http404()

这将是一个很好的做法在视图模块中放置 QuerystringRedirect ,但不是必需的。然后,您可以通过以下方式将视图添加到您的网址模式:

  urlpatterns = [
url(r'^ @,查询字符串,name =original_view),
url(r'^ permalink /(?P< id> \d +)/ $' target_view),
]


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.

解决方案

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, but it's not required. You would then add the view to your url patterns with something like:

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

这篇关于如何使用Django中urls.py的变量重定向url模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆