开发者

Passing a variable in redirect in Django

开发者 https://www.devze.com 2023-03-07 07:38 出处:网络
I\'m trying to pass a variable using the redirect function, but it is returning none. def one: # define location variable h开发者_如何学Goere

I'm trying to pass a variable using the redirect function, but it is returning none.

def one:
    # define location variable h开发者_如何学Goere
    return redirect(getting_started_info, location=location)

def getting_started_info(request, location=''):
    location = location
    ...

Could someone please tell me why the variable in the redirect is not passing?


Remember that redirect() isn't doing a direct call to your view, it's actually sending the client's browser a 302 Found status (301 Moved Permanently if you use permanent=True) with an instruction to redirect to a different URL. In this case, that URL is one that resolves to the getting_started_info view.

I believe that for this to work, there must exist a urlconf which maps to getting_started_view and which uses its location positional argument. This will most likely occur through named groups.

From the django 1.8 docs entry on redirect():

The arguments could be:

  • A model: the model’s get_absolute_url() function will be called.
  • A view name, possibly with arguments: urlresolvers.reverse will be used to reverse-resolve the name.
  • An absolute or relative URL, which will be used as-is for the redirect location.


return HttpResponseRedirect(reverse('getting_started_info', kwargs={'location': location}))


you may also pass a value of some variable using sessions or cookie.

0

精彩评论

暂无评论...
验证码 换一张
取 消