0

I am new to python and Flask. I am currently working on a project where I need to render different pages for different actions but they both need to have access to the one same object.

for example:

@app.route("/", methods =['GET','POST'])
def funtionA()
    objs = somefunction() // returns an object list
    render_template("/results/'+objs+'")

@app.route("/results/<objs>", methods =['GET','POST'])
def functionB(objs)
    for obj in objs
       print(objs[i].attr)

I am able to pass string, integer even path as a parameter. I couldn't find any references to see if passing an object is doable or not. Appreciate your responses!

Thanks, Deepak

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
  • Could you elaborate a bit more on the data you need pass from one view to the other? Is the data coming out of a DB? – Adam Dec 04 '17 at 21:20

2 Answers2

1

You could use an application global. This is accessible via flask.g

http://flask.pocoo.org/docs/0.12/api/#application-globals

Flask provides you with a special object that ensures it is only valid for the active request and that will return different values for each request.

Adam
  • 3,992
  • 2
  • 19
  • 39
0

it looks like what you need is to use flask url_for

@app.route("/", methods =['GET','POST'])
def funtionA()
    objs = somefunction() // returns an object list
    return redirect(url_for('app.functionB', objs=objs))

@app.route("/results/<objs>", methods =['GET','POST'])
def functionB(objs)
    for obj in objs
       print(objs[i].attr)  
    return "something {}".format(objs)

not also that you views functions should always return a response

it's one of the solutions to your problem, hope it will help

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73