0

According to this question you can only send data from input forms from html to Python with POST. I'm trying to figure out how to pass a value (that's actually originally contained in a dictionary that I passed in from Python) from html to Python.

My two approaches I considered (and have not figured out how to do successfully) are:

  1. Taking a look at the Flask quickstart, this should be quite simple. I'm just not sure what the syntax should look like on the html side to pass in this parkCode.
@app.route('/park/<parkCode>', methods =['GET', 'POST'])
def park(parkCode):
    return render_template('park.html', parkCode = parkCode)
  1. Alternatively, is there some way to simply send a string from html to Python without using an input form? I have yet to find a way to do this.

For reference, this is the line where I'm sending over the ```parks`` dictionary:

return render_template('search_results.html', parks=parks)

Then, in my search_results.html file:

{% for park in parks %}
    <div method = "POST" action = "/park"><a href="park.html">{{park["fullName"]}}</a></div>
{% endfor %}

But I want the to send the park["fullName"] to my Python code.

1 Answer 1

1
  1. .route decorator always handles only URL paths. Since form action is a static value in HTML, the only way to change it is to use JavaScript (for example, by changing the action attribute at submit time). If you're going to use JavaScript, you might as well then just use JavaScript to submit the request itself, which leads us to

  2. Yes, you can use AJAX to send a request. Since ES6, the easiest way to do this is fetch. The choice of whether to use a form or whether to use AJAX depends on what you want to happen after the request: a submitted form results in a new page being rendered, while an AJAX request cannot change the current page, only trigger JavaScript code (although obviously you can change the page in JavaScript should you so wish).

Basically, you can't do what you want without JavaScript. The third option that does work without JavaScript is using the form as it was meant to be used. On flask side, it involves not naming the parameter inside the route, but using request.args (for GET forms) or request.form (for POST forms):

@app.route('/park', methods =['POST'])
def park():
    parkCode = request.form.get('parkCode')
    return render_template('park.html', parkCode = parkCode)

with the accompanying HTML:

<form action="/park" method="POST">
  <input name="parkCode"/>
  <input type="submit" value="Submit"/>
</form>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. I don't quite understand your answer for 1 and 2, but regarding the third option, I don't want to send this information via form. Ultimately I'd just like to send a string from HTML to Python. Is there no "simple" way to do this?
Depends what you mean by "simple". document.querySelector('#myButton').addEventListener('click', evt => fetch('/post/' + document.querySelector('#myInput').value)) would do #2.
I see... in that case how do I use the variable I'm passing from HTML in Python? (in this case, the park["fullName"[)? Sorry, I'm very new to this so I don't understand the syntax.
The fetch code snippet from my last comment would be compatible with the code in your OP, @app.route('/park/<parkCode>', methods =['GET', 'POST']).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.