0

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":
           c,conn = connection()
           c.execute('''select * from Country''')
           data = c.fetchall ()
           c.close ()
            conn.close ()
     return render_template("view.html",data=data)

view.html

    <div class = "one1-div">
   <div class ="container">
   <form class="form-group" action="{{ url_for('SingleView') }}"          method=post>
    <table class="table">
     <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
   <tbody>
    {%for row in data%}
     <tr class="success">
      <td>{{row[0]}}</td>
      <td>{{row[1]}}</td>
      <td>{{row[2}}</td>
    </tr>
    {%endfor%}
  </tbody>
  </table>
  </form>

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 is empty noting print. Is there a better way to do this or am I missing something. Please advice or help. This is the link to where the code are. http://pastebin.com/u/itetteh/1 thanks

isaac_34
  • 1
  • 6
  • Are you sure your table name is correct? `Contintent` looks like a typo. That would raise an exception before `data` is given a value, causing your problem. – dirn Apr 19 '16 at 11:22
  • Sorry but its correct in my files. Thanks for that i fixed it here – isaac_34 Apr 19 '16 at 12:35
  • Is `execut` spelled correctly in your files, too? – dirn Apr 19 '16 at 13:42
  • Yes it is. I typed it here thats why i made some spelling mistakes. It works when i do return str([columns[0] for columns in c.fetchall()]) it print the values in the db on the page – isaac_34 Apr 19 '16 at 13:52
  • What's the full traceback? Please update your question to include that as well as the correct code. – dirn Apr 19 '16 at 14:09

0 Answers0