How to Redirect with Post Data (Django)

How to redirect with post data (Django)

If you faced such problem there's a slight chance that you might need to revise your designs.

This is a restriction of HTTP that POST data cannot go with redirects.

Can you describe what are you trying to accomplish and maybe then we can think about some neat solution.

If you do not want use sessions as Matthew suggested you can pass POST params in GET to the new page (consider some limitations such as security and max length of GET params in query string).

UPDATE to your update:)
It sounds strange to me that you have 2 web apps and those apps use one views.py (am I right?). Anyway consider passing your data from POST in GET to the proper view (in case data is not sensitive of course).

Django how can I redirect after post request result

In the template you can let the page redirect to a given URL with:

<meta http-equiv="refresh" content="5; URL={% url 'name-of-index-view' %}">
<link rel="canonical" href="{% url 'name-of-index-view' %}">

by adding that in the <head> compartment in the template that is rendered by the view that fires with the dataresult.html URL.

External django redirect with POST parameters

I suggest the following approach. In your Django view/template return form to the browser with all the parameters that you want to post as hidden form elements. As soon as the form loads the JavaScript will submit (POST) form to where ever you want.

View:

from django.shortcuts import render_to_response

def view(request):
return render_to_response('test.html', { 'foo': 123, 'bar': 456 })

Template:

<html>
<head>
<title>test</title>
<script type="text/javascript">
function load()
{
window.document.test.submit();
return;
}
</script>
</head>
<body onload="load()">
<form name="test" method="post" action="http://www.example.com">
<input type="hidden" name="foo" value={{ foo }} />
<input type="hidden" name="bar" value={{ bar }} />
</form>
</body>
</html>

Redirect a POST request with all the data to a url

Instead of redirecting on POST, you can just call the other view function directly:

@verified_email_required
def redirect_profile_page(request, pk):
page_user = get_object_or_404(User, pk=pk)
if request.method == 'POST':
return user_page(request, pk, page_user.username)
...

The other view will process and do the redirect.

If you really want to redirect, you could use HTTP status code 307 which will redirect with the same method and data.

Django redirect to url with post data

The server side cannot redirect the user and make it a post request. Based on the simple code shared, what I think you are looking for is the action attribute of the Form html tag.

DJANGO - Redirect to different page from POST with data

Your first problem is that you are not returning a response when the request method is post and the form is invalid. You can fix that by changing the indentation of your view, so that you always return a response at the end of your view.

def get_name(request):

# if this is a POST request we need to process the form data
if request.method == 'POST':
...
else:
form = NameForm()
template = loader.get_template("app/name.html")
context = {'form': form}
return HttpResponse(template.render(context, request))

If you want to redirect to the /thanks/ view, then you can use the redirect shortcut.

    if form.is_valid():
return redirect('thanks')

Note that it isn't possible to redirect and pass the form data (see this question for an explanation). You should do any processing you want with the data before redirecting. You could use the messages framework to create a message 'Thanks <name>' before redirecting.

This works because you have name='thanks' in your url pattern.

You can simplify your views by using the render shortcut. Instead of

template = loader.get_template("app/name.html")
context = {'form': form}
return HttpResponse(template.render(context, request))

you can simply do:

return render(request, "app/name.html", context)

Remember to add the imports for the shortcuts:

from django.shortcuts import redirect, render

How to post data with HttpResponseRedirect in Django?

You can get your data from POST and send it as GET parametrs. See

def response_change(self, request, obj): ### response
if "_peer-test" in request.POST:
url = request.POST.get('obj', '/export')
if url != '/export':
# because "obj" in variable url
name = url.name
url += "?name={}".format(name)
return HttpResponseRedirect(url)

Thus you can get this value in view that need to recieve it. Example:

def reciever_view(request):
name = request.GET.get('name','')
# do some with name ...


Related Topics



Leave a reply



Submit