Can't get a value of chosen radio field with flask

Viewed 27

I'm really stuck. There is a simple web app which works perfectly except of one place, where I need to use radio field. There are sliders and text area on this page and their values are passing correctly. BUT this radio field, idk...

The page conceptually looks like his:

<form method = "post">
        
{% for answer in a %}
<input type="radio" id="answer{{answer.answer_number}}" name="answers" value="{{answer.answer_number}}">
<label for="answer{{answer.answer_number}}">{{answer.answer_content}}</label><br>
{% endfor %}
        
    <div class="text-center">
    <span style="font-size: larger">Difficulty</span> 
    </div>
    <div class="align-items-center">
    <label for="trading_exp" class="float-start"> Simple </label>
    <label for="trading_exp" class="float-end"> Hard </label>
    <input type="range" name = "difficulty" class="form-range" min="1" max="5" id="trading_exp">
    </div

My app.py looks like this:

if request.method == 'POST' and form.validate_on_submit():
    a = Answer.query.filter_by(question_id = id,is_active = True, language_id = session['language_id']).all()
    result = Result(session_id = current_user.id,
                            question_id = q.question_id,
                            answer_number = request.form.get('answers'),
                            start_date = session['st_date'],    
                            finish_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                            difficulty = request.form.get('difficulty'), 
                            clarity = request.form.get('clarity'),
                            grammar = request.form.get('grammar'),
                            comment = request.form.get('comment'))
    db.session.add(result)
    db.session.commit()
    session['st_date'] = datetime.now()
    return redirect(url_for('question', id =(id+1)))
    

So if I'm using in my HTML form just {{form.answers}} from Forms.py and form.answers.data instead of request in app.py, everything works and value is storing to db. Notice, that difficulty, for example, passing the value correctly using request.get. But then I can't customize the view of the answers and can't use Bootstrap on it. But if I use radio field as in example above and try to request.get value from it it's not working. I'm trying to beat it for two days already and still can't, please help.

0 Answers
Related