Search code examples
pythonhtmltornadomulti-select

can't get multi-select field name when post (no choice)


html:

<form method="post">{% module xsrf_form_html() %}
    <input name="username" type="text">
    <select name="ss" multiple>
        <option value="1">hello</option>
        <option value="2">word</option>
    </select>
    <button type="submit">提交</button>
</form>

tornado:

class TestMultiSelectEmptyPost(BaseHandler):

    def get(self, *args, **kwargs):
        self.render('multi-select-empty-post.html')

    def post(self, *args, **kwargs):
        print self.request.arguments

browser: enter image description here

server return:

{'username': ['aaaa'], '_xsrf': ['xxx|xxx|xxx|xxx']}

multi-select field name "ss" is missing


Solution

  • To determine whether the user selected none of the options, do:

    ss = self.request.arguments.get("ss")
    

    The get method returns None if there is no value. So,

    if ss is None:
        print("User selected nothing")
    else:
        print("ss = %s" % ss)