如何实现选择,以便在用户单击提交按钮时可以访问我的代码中的选定值

时间:2014-06-21 11:03:53

标签: jquery python html python-2.7 flask

我正在制作基于Flask documentation的简单上传器,一切都在从获取文件到保存它。我的问题是如何实现select,以便当用户点击提交按钮时我可以访问我的代码中的选择值,最好用户必须选择其中一个值。

在模板中

        <div>
          <form action="upload" method="post" enctype="multipart/form-data">

            <div style="position:relative; overflow:hidden;">
            <a class='btn btn-primary' href='javascript:;'>
                Choose document
                <input type="file" style='position: absolute;top: 0;right: 0;min-width: 100%;min-height: 100%;font-size: 999px;
                text-align: right;filter: alpha(opacity=0);opacity: 0;outline: none;background: white;cursor: inherit;display: block;' 
                name="file" id ="Mfile" size="40" onchange='$("#upload-file-info").html($(this).val());'>
            </a>
            &nbsp;
            <span class='label label-info' id="upload-file-info"></span>
        </div>
        <br/>
    <br/>
    <select name = "choose" class="form-control">
      <option value="cop">Computer programming</option>
      <option value="bio">Biology</option>
      <option value="mth">Math</option>
      <option value="soc">Sociology</option>
      <option value="psy">Psychology </option>
    </select>
    <br/>
            <button type="submit" class="btn btn-success">
                <i class="icon-circle-arrow-right icon-large"></i> Submit
            </button>
      </form>
      </div>

代码

@main.route('/upload', methods=['POST'])
@login_required
def upload():
    file = request.files['file']
    ...
    ...

1 个答案:

答案 0 :(得分:1)

要在Flask中访问任何用户提供的值,您可以使用request objectrequest是与您可以用来访问的请求上下文绑定的线程本地:

  • 通过request.args查询字符串参数(例如/some/url?these=params&right=here

    request.args['these']  # "params"
    request.args['right']  # "here"
    
  • 通过request.form来自表单帖子等的URL编码的正文参数:

    <!-- in your html -->
    <form action="/do-stuff" method="post">
        <input name="these" value="params">
        <input name="right" value="here">
        <input type="submit">
    </form>
    
    
    # In your script
    request.form['these']  # "params"
    request.form['right']  # "here"
    
  • 通过request.get_json()获取JSON编码的实体,通过request.cookies获取更多Cookie数据(详见the API documentation for flask.Request