You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import os
from flask import Flask, render_template, request
from flask_dropzone import Dropzone
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config.update(
UPLOADED_PATH=os.path.join(basedir, 'uploads'),
# Flask-Dropzone config:
DROPZONE_ALLOWED_FILE_CUSTOM=True,
DROPZONE_ALLOWED_FILE_TYPE='.csv',
DROPZONE_MAX_FILE_SIZE=3,
DROPZONE_MAX_FILES=30,
DROPZONE_REDIRECT_VIEW='upload' # set redirect view
)
dropzone = Dropzone(app)
@app.route('/', methods=['POST', 'GET'])
def upload():
message = None
print(request.method, request.files)
if request.method == "POST":
# do something with the file ...
message = "done processing file"
return render_template('index.html', message=message)
if __name__ == '__main__':
app.run(debug=True)
But no 'message' is rendered. Basically the problem is that the only time the backend sees the uploaded file is when the request method is POST, but then for some reason the message=message argument does not work - nothing changes on the webpage.
The only way to pass a variable to a template is when the request method is GET but then the backend does not see the uploaded file...
Any ideas how to solve this?
The text was updated successfully, but these errors were encountered:
Dropzone.js will upload file in asynchronous AJAX request, it will not update the page with your response. If you just want to send a success message, you can return the message directly:
fromflaskimportjsonify@app.route('/', methods=['POST', 'GET'])defupload():
message=Noneprint(request.method, request.files)
ifrequest.method=="POST":
# do something with the file ...message="done processing file"returnjsonify(message=message) # <--returnrender_template('index.html')
then listen to success event on client-side to receive the response (and do something with it):
greyli
changed the title
Pass a variable to a template after file upload
Pass a variable (filename or message) to the template (frontend) after file uploaded
Apr 6, 2021
Hello. I'm unable to pass a variable to a template after a file upload. The code is modified basic example:
index.html
and the Python side
app.py
But no 'message' is rendered. Basically the problem is that the only time the backend sees the uploaded file is when the request method is
POST
, but then for some reason themessage=message
argument does not work - nothing changes on the webpage.The only way to pass a variable to a template is when the request method is
GET
but then the backend does not see the uploaded file...Any ideas how to solve this?
The text was updated successfully, but these errors were encountered: