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

Oleksandr Chalyi kerriden1 at gmail.com
Tue Apr 19 10:35:59 EDT 2016


 if request.method=='POst'

I believe it should be changed to ==  'POST'

 c.execut('''SELECT * FROM Continent''')
change to
 c.execute('''SELECT * FROM Continent''')

But actually, the problem here is that if a request method is not POST,
flask goes to return render_template("view.html", data=data), where
variable data is not declared.
You should declare data before  "if request.method == 'POST'"


2016-04-19 16:48 GMT+03:00 Unai Rodriguez <unai at sysbible.org>:

> Don't you want to use flask SQL alchemy? The code becomes much cleaner
> and less error prone.
>
> -- unai
>
> On Tue, Apr 19, 2016, at 09:38 PM, isaac tetteh wrote:
> > 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
> > _______________________________________________
> > Flask mailing list
> > Flask at python.org
> > https://mail.python.org/mailman/listinfo/flask
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160419/3520effe/attachment.html>


More information about the Flask mailing list