Chris Burgin
Chris Burgin

Reputation: 256

Python Flask Get Data Attribute From Form

Hopefully there is a very simple answer to this problem. I want to get data from a POST in flask that is not your standard textfield value. The trick to this is that I want to try and find a solution without using javascript, I could easily do that but Im attempting to only use python.

This is not my specific example but instead a simplified one.

I want to get the value of "data-status"

<form action="/myurl/" method="post">
    <div data-status="mydatahere" class="classname"></div>
</form>

Python

@app.route('/myurl/', methods=['POST'])
def myurl():
    #python to get 'data-status' value here.

Thanks so much to anyone that can provide an answer.

Upvotes: 3

Views: 4074

Answers (2)

jeffmjack
jeffmjack

Reputation: 642

You could pass it in as a URL parameter via the action attribute of your form, e.g.:

<form action="/myurl/?data-status=mydatahere" method="post">
    <div class="classname"></div>
</form>

And then pick it up in flask like so:

@app.route('/myurl/', methods=['POST'])
def myurl():
    #python to get 'data-status' value here as my_var variable:
    my_var = request.args.get('data-status')

Not sure if this works in your situation but it is how I solved a similar problem for myself :)

Upvotes: 3

iurisilvio
iurisilvio

Reputation: 4987

You can't.

If you're for some reason unable to change your HTML, I suggest you to make it on submit event.

Take a look at this other question: How to add additional fields to form before submit?

Upvotes: 1

Related Questions