vendredi 25 septembre 2020

Flask isn't getting pre-selected active checkbox [duplicate]

Using Flask, I'm trying to retrieve the option selected by the user. I want one of the buttons to be the default selection, being active when the user accesses the webpage. However, using class="active", is not enough, as request.form returns empty if the user accepts the pre-selection choice and does not click on it.

Using <link href="/static/styles/bootstrap.min.css" rel="stylesheet">, option 1 shows as pre-selected:

enter image description here

Without using <link href="/static/styles/bootstrap.min.css" rel="stylesheet">, it is possible to see that there is no real pre-section:

enter image description here

How to make a default selection of a checkbox, to be able to later request its value?

Full codes:

app.py

from flask import Flask, render_template, request, redirect

app=Flask(__name__)
app.secret_key = "secret key"

@app.route('/')
def upload_form():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def index():
    global uniqueId, nomeArquivos

    if request.method == 'POST':
    
        print(request.form)
    
        return redirect('/')

if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="/static/styles/bootstrap.min.css" rel="stylesheet">
</head>
<body>

    <form method="post" action="/" enctype="multipart/form-data">
        
        <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-primary active">
            <input type="radio" name="options" id="option1" value="option1"> option1
            </label>
            <label class="btn btn-primary">
            <input type="radio" name="options" id="option2" value="option2"> option2
            </label>
        </div>
    
        <input type="submit" value="Submit">
        
    </form>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire