简体   繁体   中英

Use Django's RedirectView with a named url

I'm trying to make my a_detail redirect to my a_detail_slug url. I want to use the named url for this but I haven't succeeded yet, this is what I've tried:

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.

a_detail_slug requires 2 params ( pk and filler ) but you pass none of them. The easiest way will be extend RedirectView:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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