Access Multiselect Form Field in Flask

前端 未结 3 2095
不知归路
不知归路 2020-12-24 01:16

I have a multiselect in html file like this:


                        
    
提交评论

  • 2020-12-24 02:14

    You want to use the getlist() function to get a list of values:

    multiselect = request.form.getlist('mymultiselect')
    

    You do not need to add [] to the name to make this work; in fact, the [] will not help, don't use it at all.

    0 讨论(0)
  • 2020-12-24 02:14

    You can create a function to get values as dictionary

    def __get_form_data(self, method='POST', compare_with=dict()):
        # Get form MiniFieldStorage items
        form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={ 'REQUEST_METHOD' : method })
        # Convert to dictionary
        return { key.rstrip('[]'):form.getlist(key) if key.endswith('[]') else form.getvalue(key) for key in form.keys() if compare_with.get(key)!=form.getvalue(key) }
    
    0 讨论(0)
  • 提交回复
    热议问题