MySQL data when retrieved with Python -


the data mysql being displayed characters should not there. using python 2.7 , django.

i tried search how fix didn't know call issue..

this how data showing: ('royal ashburn',) charfield or (5l,) integerfield (should show 5)

i have feeling has json or of effect unsure (i still new this) there way fix this?

view.py

def getcourses(request):  db = mysqldb.connect(host="localhost", user="***", passwd="***", db="golf") cur = db.cursor() cur.execute("select course_name golfapp_golfcourses") data = cur.fetchall()  """cur.close() connection.close() sys.exit()"""  return render_to_response('dbtest.html', {'data':data}) 

course.html

{% include "base.html" %} <html> <body>  <select name="list_courses"> {% data in data %}  <option>{{ data }} </option> {% endfor %} </select>   </body> </html> 

edit per comment on answer: views.py

def getallcourses(request):     db = mysqldb.connect(host="localhost", user="edwardb", passwd="edwards17",         db="golfapp")     cur = db.cursor()     cur.execute("select course_name, par_front_9, par_back_9, total_par     golfapp_golfcourses")     data1 = [course_name (course_name,) in cur.fetchall()]     data2 = [par_front_9 (par_front_9,) in cur.fetchall()]     data3 = [par_back_9 (par_back_9,) in cur.fetchall()]     data4 = [total_par (total_par,) in cur.fetchall()]  """cur.close() connection.close() sys.exit()"""      return render_to_response('golfcourses.html', {'data1':data1}, {'data2':data2}, {'data3':data3}, {'data4':data4},) 

template:

{% extends "base.html" %}  {% block content %}  <table style="margin-left:15%" class="table table-bordered">   <tbody>   <th>     <td>golf course</td>     <td>front 9</td>     <td>back 9</td>     <td>total par</td>   </th>    <tr>     {% data1 in data1 %}     <td>{{ data1 }} </td>     {% endfor %}     {% data2 in data2 %}     <td>{{ data1 }} </td>     {% endfor %}     {% data3 in data3 %}     <td>{{ data3 }} </td>     {% endfor %}     {% data4 in data4 %}     <td>{{ data4 }} </td>     {% endfor %}   </tr>  </tbody  </table> </div>  {% endblock %} 

fetchall() returns tuple of columns, each row. if requested single column, each row in data still tuple, of single element. want:

data = [course_name (course_name,) in cur.fetchall()] 

edit: render multiple columns, don't need "clean up" @ all:

data = cur.fetchall() 

you need access each column in template:

{% course_name, par_front_9, par_back_9, total_par in data %} <tr>   <td>{{ course_name }}</td>   <td>{{ par_front_9 }}</td>   <td>{{ par_back_9 }}</td>   <td>{{ total_par }}</td> </tr> {% endfor %} 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -