lundi 4 mai 2015

Sqlite insert in Flask

I have an html template that includes some Python code and HTML templates. This is a sample below.

Python:

cur1 = g.db.execute('select ID from Students')
students = [dict(ID=row[0]) for row in cur1.fetchall()]
cur2 = g.db.execute('select ID from Quizzes')
quizzes = [dict(ID=row[0]) for row in cur2.fetchall()]
if request.method == 'POST':
    if not session.get('logged_in'):
        abort(401)
    g.db.execute('insert into Results (quiz,student,grade) values '\
                 '(?,?,?)',[request.form['Quiz'],
                            request.form['Student'],
                            request.form['grade']])
    g.db.commit()
    return render_template('add_result.html',students=students,quizzes=quizzes)

HTML:

  <form action="{{ url_for('add_result') }}" method=post>
  <select name="Quiz">
    {% for quiz in quizzes %}
    <option value="{{ quiz }}">{{ quiz.ID }}</option>
    {% endfor %}
  </select>
  <select name="Student">
    {% for student in students %}
    <option value="{{ student }}">{{ student.ID }}</option>
    {% endfor %}
  </select>
  <input type="number" name="grade">
  <input type="submit" value=Submit>
  </form>

which outputs the drop down lists in the format I want (integers), however, for example, say the values quiz #1 and student #1 with a grade of 100, they are then input into SQLite as

(2, u"{'ID': 1}", u"{'ID': 1}", 100.0)

How can I get this output?:

(2,1,1,100.0)

Aucun commentaire:

Enregistrer un commentaire