Home > OS >  Pass multiple variables between pages to the html url from flask
Pass multiple variables between pages to the html url from flask

Time:01-13

I need to pass two variables in the url from one html page to another. It turned out to pass one "name" and it looks like: http://localhost/push_web?service_name=test

But I need to pass it so that it turns out something like: http://localhost/push?name=test&token=21hgsgwqyqy8218gddsyqwaw22

But this does not work, only the name variable is passed to the url, and the token is written to the cell as None. What am I doing wrong? Or is it impossible to do this in HTML?

My code what have I tried:

flask:

session['name'] = name
session['token'] = token           
return redirect(url_for('push_web'))
@app.route('/push')
def push():
    name = request.args.get('name', None)
    token = request.args.get('token', None)
    return render_template("push.html", name=request.args.get('name', None), token=request.args.get('token', None))

in html:

<a href="{{ url_for('push', name=name, token=token ) }}">Page</a>

I was expecting a page like: http://localhost/push?name=test&token=21hgsgwqyqy8218gddsyqwaw22

But I only succeeded: http://localhost/push?name=test

CodePudding user response:

This is how it is intended to work:

@app.route('/push')
def push():
    name = session.get('name', None)
    token = session.get('token', None)
    return render_template("push.html", name=name, token=token)

CodePudding user response:

I found the solution myself, in my case it worked like this:

Flask(Python): I take the variables from here:

   session['name'] = name
   session['token'] = token           
   return redirect(url_for('push_web'))

I put variables here here:

@app.route('/push') 
   def push():     
       name = request.args.get('name', None)     
       token = request.args.get('token', None)     
       return render_template("push.html", name=name, token=token)

HTML:

<a href="{{ url_for('push', name=name, token=token ) }}">Page</a>

Result: http://localhost/push?name=test&token=21hgsgwqyqy8218gddsyqwaw22

  • Related