Reputation: 3187
Suppose I have a view which will take a POST request. After the validation check pass, I need to redirect the request to another HTML/view with a request with GET method:
def view1(request):
if request.POST:
form = TempForm(request.POST)
if form.is_valid():
return redirect(request, 'view2')
def view2(request):
if request.POST:
#POST stuff here
else:
#GET stuff here
My problem is that after the form.is_valid()
, the redirect request will be passed as a POST method. My ultimate goal is to redirect the view2 with GET method.
Can I do such thing in Django?
Upvotes: 2
Views: 5497
Reputation: 408
I had a similar type of problem, needing to convert a POST request into a GET request to feed a view function.
After fiddling a little, I discovered that I only needed to set manually the request.method attribute to achieve my goal:
if request.method == 'POST':
request.method = 'GET'
return view_func(request, *args, **kwargs)
It just worked perfectly
Upvotes: 0
Reputation: 88
The user agent (the browser) decides if it gets redirected with POST or GET. Most browsers will switch from POST to GET if they get redirected. The only way I know you can get redirected and stay in POST is if you do it explicitly, such as with curl -X POST.
http://en.wikipedia.org/wiki/Post/Redirect/Get
Upvotes: 0
Reputation: 13308
You can use an HttpResponseRedirect class to redirect to any URL you like. Since it's a redirect, the request will be a GET request (POST isn't possible with http redirect - that's a restriction of the http protocol).
If you need to add GET parameters you could simply create the GET string yourself -
get_string = "?"
get_strint += "my_param=" + my_variable + "&"
get_string += "my_other_param=" + my_other_variable
return HttpResponseRedirect('/my_url/' + get_string)
Upvotes: 3