【问题标题】:How to pass image uploaded with flask dropzone to another page如何将使用flask dropzone上传的图像传递到另一个页面
【发布时间】:2019-09-04 22:29:11
【问题描述】:

所以我已经使用flask dropzone 将图像上传到uploads 文件夹中,并且我希望能够将其传递到我的makeMeme 页面,这样我就可以使用html img 标签显示该图像。这是我上传图像并尝试传递它的方式。但是,当我运行基本的 img take 并尝试引用文件时,我得到一个错误。 这是我得到的属性错误:

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")

【问题讨论】:

    标签: python html flask file-upload dropzone.js


    【解决方案1】:

    首先创建templatesstatic文件夹
    然后复制模板内的upload.html和makeMeme.html
    然后执行app.py
    我希望这能解决您的问题

    应用程序.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)
    

    上传.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>
    

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      相关资源
      最近更新 更多