简体   繁体   English

使用Django的RedirectView和一个命名的url

[英]Use Django's RedirectView with a named url

I'm trying to make my a_detail redirect to my a_detail_slug url. 我正在尝试将a_detail重定向到我的a_detail_slug网址。 I want to use the named url for this but I haven't succeeded yet, this is what I've tried: 我想使用已命名的url,但我还没有成功,这就是我尝试过的:

url(r'^a/(?P<pk>\d+)/(?P<filler>[\w-]+)/$', AList.as_view(template_name="a.html"), name="a_detail_slug"),

url(r'^a/(?P<pk>\d+)/$', RedirectView.as_view(url=reverse_lazy("a_detail_slug"),), name="a_detail"),

This is meant to catch any link with a valid pk and redirect to that page with an appended filler. 这意味着捕获具有有效pk的任何链接,并使用附加填充重定向到该页面。

a_detail_slug requires 2 params ( pk and filler ) but you pass none of them. a_detail_slug需要2个参数( pkfiller a_detail_slug ),但不传递任何参数。 The easiest way will be extend RedirectView: 最简单的方法是扩展RedirectView:

class ARedirect(RedirectView):
    def get_redirect_url(self, pk):
        filler = get_filler_somehow()
        return reverse('a_detail_slug', args=(pk, filler))

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

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