Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a variable (filename or message) to the template (frontend) after file uploaded #41

Closed
Asa-Nisi-Masa opened this issue Nov 29, 2020 · 1 comment
Labels

Comments

@Asa-Nisi-Masa
Copy link

Asa-Nisi-Masa commented Nov 29, 2020

Hello. I'm unable to pass a variable to a template after a file upload. The code is modified basic example:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flask-Dropzone Demo: Basic</title>
  {{ dropzone.load_css() }}
  {{ dropzone.style('border: 2px dashed #0087F7; margin: 10%; min-height: 400px;') }}
</head>
<body>
  {{ dropzone.create(action='upload') }}
  {{ dropzone.load_js() }}
  {{ dropzone.config() }}

  {% if message %}
    <p>{{message}}</p>
  {% endif %}
</body>
</html>


and the Python side

app.py

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?

@greyli
Copy link
Member

greyli commented Dec 8, 2020

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:

from flask import jsonify

@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 jsonify(message=message)  # <--

    return render_template('index.html')

then listen to success event on client-side to receive the response (and do something with it):

{{ dropzone.config(custom_options="success: function(file, response){ alert(response.message); }") }}

@greyli greyli added the question label Dec 8, 2020
@greyli greyli pinned this issue Apr 6, 2021
@greyli greyli closed this as completed Apr 6, 2021
@greyli 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants