Basically I'm trying to create a survey that assesses some skills a person has. I'm using flask for the website, wtforms for the RadioField, and sqlite for the database. Here's my sqlite code:
CREATE TABLE questions (
q_id INTEGER PRIMARY KEY AUTOINCREMENT,
skill TEXT NOT NULL,
question TEXT NOT NULL
);
To help me solve this you can just insert any values you want, the values don't matter.
My python code:
from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from wtforms import RadioField, SubmitField
from flask_wtf import FlaskForm
bp = Blueprint('website', __name__)
def get_questions():
questions = get_db().execute(
'SELECT skill, question FROM questions'
)
return questions
@bp.route('/questions', methods=('GET',))
def questions():
form = Quiz()
questions = get_questions()
for question in questions:
setattr(form, question["skill"], RadioField(u"{}".format(question["question"]), choices=[("0","Strongly disagree"),("0","Disagree"),("1","Neither agree nor disagree"),("2","Agree"),("3","Strongly agree")]))
return render_template('questions.html', questions=form)
This is my html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing</title>
</head>
<body>
<form method="post" action="/submit">
{% for question in questions %}
{% if question.type == "RadioField" %}
{{ question.label }}
{% for subfield in question %}
<tr>
<td>{{ subfield }}</td>
<td>{{ subfield.label }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{{ questions.submit }}
</form>
</body>
</html>
The problem is that only the submit button shows up, nothing else. If I try changing == "RadioField" to != "SubmitField", then I get the following error: 'CSRFTokenField' object is not iterable.
Edit: this is my class form:
class Quiz(FlaskForm):
submit = SubmitField()
question from:
https://stackoverflow.com/questions/65557518/csrftokenfield-object-is-not-iterable-flask-wtforms