简体   繁体   中英

Django RedirectView that takes an argument

Need a view that takes an idea from the context and returns redirect url.

This code works fine:

class FooView(RedirectView):
    def get(self, request, id, *args, **kwargs):
        ...
        return HttpResponseRedirect(reverse('foo_space:foo', urlconf='bar.system.urls'))
  • In this case is there any reason to use RecirectView at all (while not View)?
  • May be there is more appripriate way to do it? Eg using get_redirect_url?

Definitely worthwhile. Check out what RedirectView does. For one, it redirects all methods by default PUT , POST , HEAD etc. while View would throw a 405 for everything but get

And definitely override get_redirect_url to take advantage of the above.

It could be simpler via setting the pattern_name attribute only, but it looks like you need the urlconf kwarg as well so this method may not work.

https://docs.djangoproject.com/en/1.7/ref/class-based-views/base/#django.views.generic.base.RedirectView.pattern_name

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