alphanumeric alphanumeric - 20 days ago 8
HTML Question

How to get sender info from request object

With three

<a>
all calling the same function:

<a id="1" href="call" One </a>
<a id="2" href="call" Two </a>
<a id="3" href="call" Three </a>


On a back-end in python using Flask it looks like this:

@app.route("/call")
def call():
print request


Now inside of Python
call()
function I get a
request
object. Can I use this
request
to get idea which of three
<a>
were clicked to call the function?

Answer

You can't get information about cliked link from request. You have to add unique argument to every link ie. href="call?number=1" or href="call/1" and then you can get number in Flask.

href="call?number=1" needs

@app.route("/call")
def call():
    print request.args.get('number', 'no number!')

see: http://flask.pocoo.org/docs/0.11/quickstart/#the-request-object

href="call/1" needs

@app.route("/call/<int:val>")
def call(val):
    print val

see: http://flask.pocoo.org/docs/0.11/quickstart/#variable-rules

Comments