gpt4 book ai didi

upload - ImportError : No module named flask. ext.uploads.UploadSet

转载 作者:行者123 更新时间:2023-12-03 17:18:57 31 4
gpt4 key购买 nike

关注 documentation我已经安装了 Flask-Upload。尝试导入时:from flask.ext.uploads import UploadSet, IMAGES它引发了一个错误:

Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/home/proj/app/__init__.py", line 38, in <module>
from app.admin.views import admin
File "/home/proj/app/admin/views.py", line 14, in <module>
from flask.ext.uploads import UploadSet, Images
ImportError: cannot import name Images

问题是什么?

最佳答案

Flask 提供了一个更简单的选项,无需扩展。请参阅以下文档。
http://flask.pocoo.org/docs/0.10/patterns/fileuploads/

这是我遵循链接中的详细信息的实现

进口

import os
from werkzeug import secure_filename
from flask import send_from_directory

#app config
UPLOAD_FOLDER = os.path.dirname(__file__)
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','doc','docx','xls'])


app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(UPLOAD_FOLDER,'uploads') # uploads folder should exists in the server

#function that checks the file extension
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

#function that handles the document upload page
@app.route('/input_docs/<contract_id>', methods=['GET', 'POST'])
def input_docs(contract_id):

if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], contract_id+'_'+ filename))
else:
flash("Not a valid file %s" % file.filename)


contract = Contracts.query.filter_by(id = contract_id).first()
try:
attachments = [file[file.find('_')+1::] for file in os.listdir(app.config['UPLOAD_FOLDER']) if file.startswith(contract_id+'_')] # Strip out the contract id for from display

except OSError as err:
attachments = []

doc_form = InputDocsForm(obj=contract)
return render_template('input_docs.html',title = 'Input Docs', form=doc_form,attachments=attachments)

处理选择文档下载的函数
@app.route('/download_docs/<filename>')
def download_docs(filename):

app.logger.info("Downloading %s"% os.path.join(app.config['UPLOAD_FOLDER'], filename))
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)

//客户端模板
<div class="row">
<div class="span3">
<p><input type=file name=file>
</div>
<div class="span3">
<input type=submit value=Upload>
</div>
<div class="span3">
<a href="{{ url_for('contracts_list') }}"> <input class="btn btn-primary" type="button" value="{{ _('Close') }}"></a>
<input id = 'download' class="btn btn-primary" type="button" value="{{ _('Download') }}">
</div>

</div>

<table class="table">
<thead>
<tr>
<th>Select</th>
<th>Attachment</th>
</tr>
</thead>
<tbody>

{% for attachment in attachments %}
<tr>
<td >{{ attachment }}</td>
<td id = "sel" onclick = 'onSelectDoc( "{{attachment}}" )'> {{ form.sel ( type ="checkbox" ) }} </td>

</tr>

{% endfor %}

</tbody>
</table>

//客户端javascript
<script>
var Attachments = [];

$( document ).ready(function(){
$( "#download" ).click(function() {
if (Attachments.length > 0){
var contract_id = document.getElementById('id').value;
var url = flask_util.url_for('download_docs' ,{ filename:contract_id+"_"+Attachments[0] } );
var k = Attachments.length;
while (k--) Attachments.shift();

$(location).attr('href',url);



}
else{
alert("Select at least one attachment to download");
return;

}

});



function onSelectDoc(attachment){


if (attachment.length>0) {
var i = Attachments.indexOf(attachment)
if (i>=0 ) {
Attachments.splice(i,1) ; //remove the attachment on unselect
return;
}
}
Attachments.push(attachment);

}

</script>

关于upload - ImportError : No module named flask. ext.uploads.UploadSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968242/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com