【问题标题】:Remembering selected dropdown values Python/Flask记住选定的下拉值 Python/Flask
【发布时间】:2016-12-18 18:35:09
【问题描述】:
            <option value="1" {{ selected }}>1</option>
            <option value="2" {{ selected }}>2</option>
            <option value="3" {{ selected }}>3</option>
            <option value="4" {{ selected }}>4</option>

我有一个带有几个下拉菜单的烧瓶应用程序。我想在用户提交表单时传递选定的值,以便他们在下一页上看到他们之前的选择。

有人可以提供一个简单的例子吗?我只是无法概念化如何实现这一点。

谢谢

【问题讨论】:

    标签: html python-2.7 flask


    【解决方案1】:

    Python Flask 视图

    @app.route('/form/')
    def form():
        # list of tuples representing select options
        choices = [(str(x), str(x)) for x in range(1, 20)]
        # test if value was passed in (e.g. GET method), default value is 1
        selected = request.args.get('choice', '1')
        # application 'state' variable with default value and test
        state = {'choice': selected}
        return render_template('view_form.html', choices=choices, state=state)
    

    在 Jinja 模板中:

    {% for row in choices %}
    <option value="{{ row[0] }}"{% if row[0] == state.choice %} selected{% endif %}>{{ row[1] }}</option>
    {% endfor %}
    

    【讨论】:

    • 我试过了,当我选择任何选项时,提交表单,下一页将选择重置为 1。
    • 改变这个时它会起作用:selected = str((request.form['choz'])) #selected = request.args.get('choice', '1')
    • @abigperson 我对使用 Flask 和 python 的下拉菜单也有疑问。介意在这里帮助我:stackoverflow.com/questions/49628274/…
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多