python - Flask Odd Behavior w/ Folder Creation/File Uploading -


i'll preface saying i'm new flask (this being first project) , i'm interested in hacking together, rather best practice.

i have code fails create folders named user in pictures directory. i've tried searching answers here no avail can 3 of these things work in harmony. function in question.

@app.route('/', methods = ["get","post"]) def upload_file():     if request.method == 'post':         file = request.files['file']         if file , allowed_file(file.filename):             filename = secure_filename(file.filename)             foo = request.form.get('name')             if not os.path.exists("/pictures/directory"): os.makedirs("/pictures"+foo)             app.config["upload_folder"] = "/pictures" + foo             file.save(os.path.join(app.config['upload_folder'], filename))     else:         return render_template("upload.html")     return render_template("index.html") 

if interested in taking @ why upload.html renders first (which expected) "continue" button fails render index.html, i'd appreciative.

here's repo if anyone's curious rest of it: https://bitbucket.org/dillon9/ss

edit 1: both of have semi-functional frontend , functional backend. new code pushed. wish accept both of answers

it's because foo variable doesn't hold value user has given. first name user specified

foo = request.form.get('name') 

but assign else same variable before using it

foo = "/directory/" 

edit: right directory being created in c:\ or something. change code this

    @app.route('/', methods=['get', 'post'])     def upload_file():         if request.method == 'post':             file = request.files['file']             if file , allowed_file(file.filename):                 filename = secure_filename(file.filename)                 foo = request.form['name']                 path = os.path.dirname(os.path.abspath(__file__)) + "/pictures/"+foo                 if not os.path.exists(path):                               os.makedirs(path)                 app.config["upload_folder"] = path                 file.save(os.path.join(app.config['upload_folder'], filename))         else:             return render_template("upload.html")         return render_template("index.html") 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -