refugehome
refugehome

Reputation: 343

How to get the data from HTML <option> value in Flask?

I am trying to create a filter that is used to sort a list based on the selected options. I do not intend to wrap the select option in a 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>

I'm trying to figure out how to get the value in Flask and use the data to query the database. I tried the code below but it did not work.

@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 ?

Upvotes: 0

Views: 919

Answers (1)

Yasser Mohsen
Yasser Mohsen

Reputation: 1480

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')

Upvotes: 1

Related Questions