简体   繁体   中英

how to get data from HTML options value in flask

Hi I am trying to create a filter that is used to sort list based on the selected options. I do not intend to wrap the select option on form tag

      <select class="custom-select mr-sm-6" id="inlineFormCustomSelect">
        <option value="new" selected>New</option>
        <option value="old">Old</option>
        <option value="randow">Random</option>
        <option value="more">More Videos</option>
        <option value="less">Less Videos</option>
      </select>
    </div>

Trying to figure out how to get the value in flask and used the data to query the database. I tried the value below but not worked.

@main.route("/filter", methods=['GET'])
def filter():
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(page=page, per_page=10)
    if request.args.get('inlineFormCustomSelect') == 'new':
        """
        value = request.args.get('inlineFormCustomSelect')
        posts = Post.query.order_by(Post.timestamp.desc()).paginate(page=page, per_page=10)
        """
        flash('yeeeeeeeeeeeeeeeeeeeeeee')
        return render_template('home.html', posts=posts)
    else:
        flash('nooooooooooooooooooooooo')
        return render_template('home.html', posts=posts)

What am I doing wrong?

You have to add a name attribute to your select element:

<select class="custom-select mr-sm-6" name="myselect" id="inlineFormCustomSelect">
...

and then use that name value to get its value, you cannot use your id value

request.args.get('myselect')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM