Index out of range using Flask/Python
<p>I am working on a web app and running into some issues with the following code. I have a database where I need to update a value. When I try to update the value I am getting an index out of range error, however when I run the code with just python not using the web part it runs and performs as expected.. I cannot figure out what is changing when I try it in part with flask and an HTML form. Here is my code to update the database;</p>

<p>EDIT:: After more testing it seems there is some issue with the form pushing the data I need back to /poplesson. If change these lines;</p>

<pre><code>user_id = request.form['clientid']
pck_id = request.form['packageid']
</code></pre>

<p>to</p>

<pre><code>user_id = 1
pck_id = 1
</code></pre>

<p>it will work as planned, however that is not what I need as I need those ID's to be pulled from the DB.</p>

<pre><code>@app.route("/poplesson", methods=['GET', 'POST'])
def poplesson():
if request.method == 'GET':
    return render_template('view_packages.html')
elif request.method == 'POST':
    con = sqlite3.connect('utpg.db')
    db = con.cursor()
    user_id = request.form['clientid']
    pck_id = request.form['packageid']
    package = db.execute("SELECT * FROM packages WHERE client_id = ?;", (user_id, ))
    result = package.fetchall()
    privatelessons = result[0][4] ##Without flask this run, with flask this  throws an index error.
    pop = privatelessons - 1
    db.execute("UPDATE packages SET privatelesson = ? WHERE client_id = ? AND pck_id = ?;", (pop, user_id, pck_id, ))
    con.commit()
    return render_template('view_packages.html')
</code></pre>

<p>This is the code I am using to display the HTML and get the value from the forms..</p>

<pre><code>&lt;table&gt;
{% for col in items %}
&lt;tr&gt;
&lt;td&gt;{{ col['pck_id'] }}&lt;/td&gt;
&lt;td&gt;{{ col['client_id'] }}&lt;/td&gt;
&lt;td&gt;{{ col['date'] }}&lt;/td&gt;
&lt;td&gt;{{ col['price'] }}&lt;/td&gt;
&lt;td&gt;{{ col['privatelesson'] }}&lt;/td&gt;
&lt;td&gt;{{ col['shortgamelesson'] }}&lt;/td&gt;
&lt;td&gt;{{ col['playinglesson'] }}&lt;/td&gt;
&lt;td&gt;{{ col['notes'] }}&lt;/td&gt;
&lt;td&gt;&lt;form class="form-container" action="/deletepackage" method="POST"&gt;
&lt;input class="form-field"  value="{{ col['pck_id'] }}" name="packageid" /&gt;  &lt;br /&gt;
    &lt;input class="submit-button" type="submit" value="DELETE Package" /&gt;  &lt;/form&gt;
 &lt;/td&gt;
 &lt;td&gt;&lt;form class="form-container" action="/poplesson" method="POST"&gt;
 &lt;input class="form-field"  value="{{ col['client_id'] }}" name="clientid" /&gt;  &lt;br /&gt;
    &lt;input class="form-field"  value="{{ col['pck_id'] }}" name="packageid" /&gt;
    &lt;input class="submit-button" type="submit" value="Subtract Private Lesson" /&gt;&lt;/form&gt;&lt;/td&gt;
&lt;/tr&gt;
{% endfor %}
&lt;/table&gt;
&lt;/body&gt;
</code></pre>

<p>if I do just this part in python, it runs as expected and using a DB viewed I can see the DB is updated. Any thoughts?</p>

<pre><code>    con = sqlite3.connect('utpg.db')
    db = con.cursor()
    user_id = request.form['clientid']
    pck_id = request.form['packageid']
    package = db.execute("SELECT * FROM packages WHERE client_id = ?;", (user_id, ))
    result = package.fetchall()
    privatelessons = result[0][4] ##Without flask this run, with flask this  throws an index error.
    pop = privatelessons - 1
    db.execute("UPDATE packages SET privatelesson = ? WHERE client_id = ? AND pck_id = ?;", (pop, user_id, pck_id, ))
    con.commit()
</code></pre>