【问题标题】:Flask isn't getting pre-selected active checkbox [duplicate]Flask 没有得到预选的活动复选框 [重复]
【发布时间】:2021-01-12 06:36:02
【问题描述】:

使用 Flask,我正在尝试检索用户选择的选项。我希望其中一个按钮成为默认选择,在用户访问网页时处于活动状态。但是,使用class="active" 是不够的,因为如果用户接受预选选项并且没有点击它,request.form 将返回空。

使用<link href="/static/styles/bootstrap.min.css" rel="stylesheet">,选项 1 显示为预选:

不使用<link href="/static/styles/bootstrap.min.css" rel="stylesheet">,可以看出没有真正的前置部分:

如何对复选框进行默认选择,以便以后能够请求其值?

完整代码:

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>

【问题讨论】:

    标签: python forms flask checkbox request


    【解决方案1】:

    只需将其设置在您的模板文件中

    <input type="radio" name="options" id="option1" value="option1" checked="checked">
    

    或者干脆

    <input type="radio" name="options" id="option1" value="option1" checked>
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 1970-01-01
      • 2015-11-18
      • 2022-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多