[Flask] Creating a bootstrap table with values from mysql using flask

isaac tetteh itetteh34 at hotmail.com
Tue Apr 19 09:38:15 EDT 2016


hello guys I am trying to create a bootstrap table with data from MySQL database. I am getting an error local variable data referenced before assignment. Please help my code is as below. 

app.py code 

    @app.route("viewSingle", methods=['POST','GET'])
    def Singleview():
        if request.method=='POst':
            if request.form['submit']=="view continent':
                try:
                    c,conn=connection()
                    c.execut('''SELECT * FROM Continent''')
                    data=c.fetchall()
                except Error:
                    flash("something wrong happend")
                finally:
                    c.close()
                    conn.close()
        return render_template("view.html", data=data)
view.html

    <div class= "container">
    <form class=form-group" action="{{url_for('SingleView')}}" method = POST>
     <tables class="table">
      <thead>
         <tr>
          <th>header1</th>
          <th>header2</th>
         </th>
       </thead>
      {%for row in data%}
        <tbody>
          <tr class ="success">
            <td>{row[1]}</td>
            <td>{row [1]}</td>
          </tr>
        </tbody>
       </table>
    </form>
    </div>
      
It seems to not be working I was hoping on getting the value [0] and [1] from the tuple( data= c.fetchall()) into the table but I get the error local variable 'data' referenced before assignment.But if I change the return render_template("view.html", data=data) to he return render_template("view.html") its runs with no error but just that the </td></td>is empty noting print. Is there a better way to do this or am I missing something. Please advice or help. Thanks 


Sent from my iPhone


More information about the Flask mailing list