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

Anthony Ford ford.anthonyj at gmail.com
Tue Apr 19 10:51:06 EDT 2016


Yeah, I just came to the same conclusion. Though given the other typos,
that may just be translation error to the email.


   - At if request.form['submit']=="view continent' , you have mismatched
   quotes around *view continent. *You have one double and one single
   quote.
   - You're also missing a leading slash on the route.
   - You are missing the {%endfor%} in your template
   - You should also wrap only the table row in your for loop (within your
   template), instead of the whole table body.
   - The url_for in the template has the wrong capitalization ("SingleView"
   vs "Singleview")
   - Jinja uses {{ }} instead of { } for printing vars.

But I assume most of these are from quickly generating a "sanitized" demo
for us to look at.
I recommend you use a pastebin or gist next time. Also try and send a full
working demo if possible. It makes it easier for someone else to download
the files and try things.

Good luck! Hope you figure out everything. Trial and error like this is the
best way to learn. We've all been there.


Anthony Ford,
KF5IBN,
ford.anthonyj at gmail.com

On Tue, Apr 19, 2016 at 10:35 AM, Oleksandr Chalyi <kerriden1 at gmail.com>
wrote:

>  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
>>
>
>
> _______________________________________________
> 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/e01e4f99/attachment-0001.html>


More information about the Flask mailing list