0

I'm trying to create a dynamic number of HTML input text fields, where the number comes posted from another form. But Flask interprets the form number 10 as 1 0, i.e. 2 (string length).

The first form:

<form action="/new_vocals" method="post">
  <input type="text" name="count">
  <input type="submit">

The Flask app route:

@app.route("/new_vocals", methods=['POST', 'GET'])
def new_vocals():
voc_count = request.form['count']
return render_template("new_vocals.html", count=voc_count)

Trying to use voc_count in a for loop, I get 2 input types with the value 10, 3 with 100 and so on.

There has to be a way to get Flask to interpret 10 as a string?

freginold
  • 3,946
  • 3
  • 13
  • 28

1 Answers1

0

You can use float() to parse your voc_count variable (which is a string) as a number, and then use int() to further convert it to an integer value. Once you've done that, you should be able to base a for loop around the value of voc_count.

voc_count = int(float(request.form['count']))
freginold
  • 3,946
  • 3
  • 13
  • 28
  • I get the response "'int' object is not iterable" when using a for loop on the variable. Any other suggestions on how to accomplish the same task? I guess there are tons of ways using Javascript but I hardly know Python.. – Stefan Fasth Nov 09 '17 at 19:25
  • Hmm, have you tried it as a `float` variable? Can't imagine that would work if `int` doesn't, but who knows? Did you take a look at the [Parse String to Float or Int](https://stackoverflow.com/questions/379906/parse-string-to-float-or-int) answers? – freginold Nov 10 '17 at 03:33