pyRN
pyRN

Reputation: 13

Flask request issue

I'm making different sized tables base off a given list and it is generated via a for loop with flask in HTML. The issue that I am having is that I'm not sure how to retrieve the values that are being entered into the type="number" form. Here is an example of my code.

HTML code:

<table>
    <tr>
        <th><h1>Name</h1></th>
        <th><h1>Number</h1></th>
    </tr>
    {% for i in range(names|length) %}
        <tr>
            <td><h2>{{ names[i] }}</h2></td>
            <td><form>
                    <input type="number" name="{{ i }}" placeholder="0" step="1" min="0" max="10" formmethod="post">
                </form></td>
            <td><form>
                    <input type="submit" class="btn" name="save_button" value="Save" formmethod="post">
                </form></td>
        </tr>
    {% endfor %}
</table>

Flask code:

@app.route('/', methods=['GET', 'POST'])
def phone():
    names = ['Jacob', 'Joe', 'Billy', 'Sue', 'Sally']

    if request.form.get('save_button'):
        for name in names:
            print(request.form.get('name')

    return render_template('phone.html', names=names)

The only thing returned is "None." Any help is greatly appreciated.

Upvotes: 1

Views: 75

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113930

try request.form.get("0")

its because your inputs name is not "name" so there is no value in request.form.get("name")

<input type="number" name="{{ i }}" placeholder="0" step="1" min="0" max="10" formmethod="post">

sets the name to whatever i is (ie a value from 0..N)..,

Upvotes: 1

Related Questions