0

So I have uploaded an image into the uploads folder with flask dropzone and I want to be able to pass it to my makeMeme page so I can then display that image using the html img tag. Here is how I upload my image, and attempt to pass it. However when I run a basic img take and try to reference file I get an error. Here is the Attribute Error I get:

AttributeError: 'NoneType' object has no attribute 'filename'

Here is my main python page.

import os
from flask import Flask, render_template,request, url_for
from flask_dropzone import Dropzone


app = Flask(__name__)
app.secret_key = 'key'

dir_path = os.path.dirname(os.path.realpath(__file__))



app.config.update(
    UPLOADED_PATH=os.path.join(dir_path, 'uploads'),
    # Flask-Dropzone config:
    DROPZONE_ALLOWED_FILE_TYPE='image',
    DROPZONE_MAX_FILE_SIZE=3,
    DROPZONE_MAX_FILES=20,
    DROPZONE_UPLOAD_ON_CLICK=True
)
app.config['DROPZONE_REDIRECT_VIEW'] = 'makeMeme'

dropzone = Dropzone(app)

@app.route('/upload', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        f = request.files.get('file')
        file = f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
    return render_template('upload.html', file_name = file)

@app.route('/makeMeme', methods=['POST', 'GET'])
def makeMeme():
    return render_template("makeMeme.html")
GuitarGuru
  • 45
  • 6

1 Answers1

2

First create templates and static folder
Then copy the upload.html and makeMeme.html inside templates
Then execute the app.py
I hope this will solve your problem

app.py

import os
from flask import Flask, render_template,request, url_for
from flask_dropzone import Dropzone

app = Flask(__name__)
app.secret_key = 'key'

dir_path = os.path.dirname(os.path.realpath(__file__))


app.config.update(
    UPLOADED_PATH=os.path.join(dir_path, 'static'),
    # Flask-Dropzone config:
    DROPZONE_ALLOWED_FILE_TYPE='image',
    DROPZONE_MAX_FILE_SIZE=3,
    DROPZONE_MAX_FILES=20
)
app.config['DROPZONE_REDIRECT_VIEW'] = 'makeMeme'

dropzone = Dropzone(app)

filename = None

@app.route('/upload', methods=['POST', 'GET'])
def upload():
    global filename
    file = None
    if request.method == 'POST':
        f = request.files.get('file')
        file = f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
        filename = f.filename
    return render_template('upload.html')

@app.route('/makeMeme', methods=['POST', 'GET'])
def makeMeme():
    global filename
    return render_template("makeMeme.html", file_name = filename)

app.run(debug=True)

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flask-Dropzone Demo</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() }}
</body>
</html>

makeMeme.html

<!DOCTYPE html>
<html>
    <img src="{{url_for('static',filename=file_name)}}" alt="Fail">
</html>
Dragon
  • 467
  • 2
  • 5
  • 13