Skip to content

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

Description

@Asa-Nisi-Masa

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions