python - 使用单个服务器端变量处理多个复选框

标签 python html checkbox

我有以下 HTML 代码:

<form method="post">
              <h5>Sports you play:</h5>
                <input type="checkbox" name="sports_played" value="basketball"> basketball<br>
                <input type="checkbox" name="sports_played" value="football"> football<br>
                <input type="checkbox" name="sports_played" value="baseball"> baseball<br>
                <input type="checkbox" name="sports_played" value="soccer"> tennis<br>
                <input type="checkbox" name="sports_played" value="mma"> MMA<br>
                <input type="checkbox" name="sports_played" value="hockey"> hockey<br>

                <br> 

                    <input class="btn" type="submit">

</form>

理想情况下,我希望有以下 python 服务器端代码:

class MyHandler(ParentHandler):
    def post(self):
        sports_played = self.request.get('sports_played')
        #sports_played is a list or array of all the selected checkboxes that I can iterate through

我尝试通过创建 HTML sports_played 名称和数组 sports_played[] 来做到这一点,但这没有做任何事情,现在它总是返回第一个选择的项目。

这可能吗?实际上,我只是不想为每个复选框都执行 self.request.get('HTML_item') 以防万一我需要更改 HTML 我不想更改 python。

谢谢!

最佳答案

答案显示在 webapp2 docs for the request object 中:

self.request.get('sports_played', allow_multiple=True)

或者你可以使用

self.request.POST.getall('sports_played')

关于python - 使用单个服务器端变量处理多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745456/

相关文章:

python - 导入错误 : cannot import name _distributor_init

python - 将句子中的数字转换为python中的单词

javascript - 使用 Angularjs 动态更改复选框

html - CSS:在另一个 float 列下 float 一个列

javascript - 使用复选框实时更新 UI

javascript - Jquery 复选框更改事件未触发

python - 在 Unix 和 Windows 上使用 Python 获取用户的 Documents 文件夹

Python通过ref访问一个对象/需要打标签

HTML5 Canvas : How can I check if a stroke is crossing an other stroke?

html - ASP.Net 中的 CSS 图标字体