Get the value of a checkbox in Flask

后端 未结 2 2006
深忆病人
深忆病人 2020-12-08 08:14

I want to get the value of a checkbox in Flask. I\'ve read a similar post and tried to use the output of request.form.getlist(\'match\') and since it\'s a list

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 08:26

    I found 4 ways to do that: Just to summarize:

    # first way
    op1 = request.form.getlist('opcao1') # [u'Item 1'] []
    op2 = request.form.getlist('opcao2') # [u'Item 2'] []
    op3 = request.form.getlist('opcao3') # [u'Item 3'] []
    
    # second
    op1_checked = request.form.get("opcao1") != None
    op2_checked = request.form.get("opcao2") != None
    op3_checked = request.form.get("opcao3") != None
    
    # third
    if request.form.get("opcao3"):
        op1_checked = True
    
    # fourth
    op1_checked, op1_checked, op1_checked = False, False, False
    if request.form.get("opcao1"):
        op1_checked = True
    if request.form.get("opcao2"):
        op2_checked = True
    if request.form.get("opcao3"):
        op3_checked = True
    
    # last way that I found ..
    op1_checked = "opcao1" in request.form
    op2_checked = "opcao2" in request.form
    op3_checked = "opcao3" in request.form
    

提交回复
热议问题