samedi 29 novembre 2014

python - Django - comment passer une Variable à l'aide HttpResponseRedirect - Stack Overflow


#view1

...
p = Model_Name.objects.get(pk=1)
var_to_send = p.field_in_model
return HttpResponseRedirect(reverse('namespace:name'))

#view2

def View2(request, var)
#do something to var

I want to be able to send var_to_send to View2.




How about HttpResponseRedirect(reverse('namespace:name', args=(var_to_send,)))




HttpResponseRedirect performs a HTTP 301 redirect, changing the url in the browser's bar.


You can pass the var as args and reverse will build the url to match your patterns. See the following answer related to this http://stackoverflow.com/a/13202435/948416


But also you can call the View2 function and return that result instead of the redirect:


...
p = Model_Name.objects.get(pk=1)
var_to_send = p.field_in_model
return View2(request, var_to_send)

The second approach is better for me because it doesn't require any additional HTTP request.



#view1

...
p = Model_Name.objects.get(pk=1)
var_to_send = p.field_in_model
return HttpResponseRedirect(reverse('namespace:name'))

#view2

def View2(request, var)
#do something to var

I want to be able to send var_to_send to View2.



How about HttpResponseRedirect(reverse('namespace:name', args=(var_to_send,)))



HttpResponseRedirect performs a HTTP 301 redirect, changing the url in the browser's bar.


You can pass the var as args and reverse will build the url to match your patterns. See the following answer related to this http://stackoverflow.com/a/13202435/948416


But also you can call the View2 function and return that result instead of the redirect:


...
p = Model_Name.objects.get(pk=1)
var_to_send = p.field_in_model
return View2(request, var_to_send)

The second approach is better for me because it doesn't require any additional HTTP request.


0 commentaires:

Enregistrer un commentaire