如何将使用 flask dropzone 上传的图像传递到另一个页面
How to pass image uploaded with flask dropzone to another page
所以我已经使用 flask dropzone 将一张图片上传到上传文件夹,我希望能够将它传递到我的 makeMeme 页面,这样我就可以使用 html img 标签显示该图片。这是我上传图片并尝试通过的方式。但是,当我 运行 一个基本的 img 获取并尝试引用文件时,我得到了一个错误。
这是我得到的属性错误:
AttributeError: 'NoneType' object has no attribute 'filename'
这是我的 python 主页。
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")
首先创建templates
和static
文件夹
然后在模板
中复制 upload.html 和 makeMeme.html
然后执行 app.py
我希望这会解决你的问题
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>
所以我已经使用 flask dropzone 将一张图片上传到上传文件夹,我希望能够将它传递到我的 makeMeme 页面,这样我就可以使用 html img 标签显示该图片。这是我上传图片并尝试通过的方式。但是,当我 运行 一个基本的 img 获取并尝试引用文件时,我得到了一个错误。 这是我得到的属性错误:
AttributeError: 'NoneType' object has no attribute 'filename'
这是我的 python 主页。
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")
首先创建templates
和static
文件夹
然后在模板
中复制 upload.html 和 makeMeme.html
然后执行 app.py
我希望这会解决你的问题
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>