Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing variables to a template on a redirect in python

I am relatively new to Python so please excuse any naive questions.

I have a home page with 2 inputs, one for a "product" and one for an "email." When a user clicks submit they should be sent to "/success" where it will say: You have requested "product" You will be notified at "email"

I am trying to figure out the best way to pass the "product" and "email" values through the redirect into my "/success" template. I am using webapp2 framework and jinja within Google App Enginge.

Thanks

like image 698
Nic Meiring Avatar asked Jul 19 '12 16:07

Nic Meiring


People also ask

How do you pass a variable in redirect?

Click Action > URL Redirect. Give your action an internal title (something that makes sense to you in the survey). Enter the link to the site in the URL field. Scroll to the Fields To Pass and select the question, hidden value, or URL Variables you would like to pass in the Question to Send dropdown.

How do I pass data in a redirect Flask?

To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters. Then we can call url_for with the parameters. to add the /found/<email>/<list_of_objects> route that maps to the found function. In it, we get the the URL parameters from the found function's parameters.

What is the difference between Render_template and redirect?

redirect returns a 302 header to the browser, with its Location header as the URL for the index function. render_template returns a 200, with the index. html template returned as the content at that URL.

What is a more efficient way to pass variables from template to view in Django?

Under normal usage the standard is to use URL (GET) variables when you are retrieving data from the server and to use Form (POST) variables when you want to manipulate (edit/delete) data on the server.


1 Answers

When you do your redirect, include your email and product variables in the redirect. In Google appp engine, using webapp2, your current redirect probably looks like:

self.redirect('/sucess')

Instead, you can add the variables in the URL as follows:

self.redirect('/success?email=' + email + '&product=' + product)

The above URL would look like '/[email protected]&product=this_product' after concatenating the values.

The handler for /success could then get those values with:

email = self.request.get('email')
product = self.request.get('product')
like image 151
Aaron Hampton Avatar answered Sep 19 '22 10:09

Aaron Hampton