我正在根据Flask文档制作一个简单的上载器,从获取文件到保存文件,所有工作都在进行。我的问题是如何实现选择,以便当用户单击提交按钮时我可以在代码中访问选择的值,最好用户必须选择其中一个值。
在模板中
<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>
<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']
...
...
要访问Flask中用户提供的任何值,可以使用request
object。request
是绑定到请求上下文的线程局部,可用于访问:
通过request.args
(例如/some/url?these=params&right=here
)查询字符串参数
request.args['these'] # "params"
request.args['right'] # "here"
来自表单帖子等的URL编码主体参数,例如request.form
:
<!-- 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"
通过JSON编码的主体request.get_json()
,通过request.cookies
和的cookie数据(在的API文档中有flask.Request
详细信息)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句