如何从烧瓶中的HTML选项值中获取数据

问题描述 投票:0回答:1

[您好,我正在尝试创建一个过滤器,该过滤器用于根据所选选项对列表进行排序。我不打算将选择选项包装在表单标签上

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

试图弄清楚如何在flask中获取值,并使用数据查询数据库。我尝试了下面的值,但没有用。

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

我在做什么错?

python flask get option
1个回答
0
投票

您必须向您的选择元素添加name属性:

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

然后使用该name值获取其值,您不能使用您的id

request.args.get('myselect')
© www.soinside.com 2019 - 2024. All rights reserved.