2014-07-26 4 views
1

Après le documentation, j'ai installé Flask-Upload. en essayant de l'importer: from flask.ext.uploads import UploadSet, IMAGES il soulève une erreur:ImportError: aucun module nommé flask.ext.uploads.UploadSet

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 

Quel est le problème au sujet?

+0

apparemment il y a 2 différentes extensions deux appelé Flask-uploads par différents auteurs. I_ಠ – endolith

Répondre

1

Essayez (notez le point omis)

flaskext.uploads import UploadSet, IMAGES 

ajouts Erlenmeyer ou Flask a changé le chemin

Edit:

Les importations sont casesensitive. Vous devriez importer IMAGES à la place.

+0

J'ai désinstallé le paquet et l'ai réinstallé. Et ça a marché. Je vous remercie. – Max

1

Flask offre une option plus simple sans besoin d'extension. Voir la documentation ci-dessous. http://flask.pocoo.org/docs/0.10/patterns/fileuploads/

Voici ma mise en œuvre suivant les détails dans le lien

Importations

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

qui gère sélectionnez Télécharger le document

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

// modèle côté client

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

// côté client 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>