在Flask中重定向页面时遇到问题

时间:2019-05-03 06:33:28

标签: python flask

我有一个包含app.py,模板文件夹(索引和结果)的代码。 我正在使用dropzone上传我的图像/视频。 我在上传图像/视频时遇到问题。我将其上传后,页面不会继续显示results.html来显示上传的图像。相反,它停留在同一页面上。但是图像/视频保存在我的目录中。 请浏览一下。

app.py:

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
    <div class="chat-popup">
       <div class="chat-history">   
        <ul id='chat-items'></ul>  
       </div>

       <div class="message">

        <!------------Message to be sent----------------------->
           <form id='form' method='POST'> 
             <input type="hidden" name="">
             <input id="id_message" type="text">        
             <input type='submit' class='btn btn-primary'/>
          </form>   
        <!------------------------------------------------------------------>
        </div>
    </div>
</body>
</html>
<script src="myscript.js"></script>
<script> 
    $( document ).ready(function() {
        var endpoint = $(".chat-popup").attr("path");
        alert(endpoint);
        var  socket = new ReconnectingWebSocket(endpoint);
    });
</script>

index.html:

import os

from flask import Flask, render_template, request, session, redirect, url_for
from flask_dropzone import Dropzone
from flask_uploads import UploadSet

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_MAX_FILE_SIZE=1024,  # set max size limit to a large number, here is 1024 MB
    DROPZONE_TIMEOUT=5 * 60 * 1000  # set upload timeout to a large number, here is 5 minutes
)
app.config['SECRET_KEY'] = 'supersecretkeygoeshere'
app.config['DROPZONE_REDIRECT_VIEW'] = 'results'
dropzone = Dropzone(app)


@app.route('/', methods=['POST', 'GET'])
def index():
    #set session for image results
    if "filename" not in session:
        session['filename'] = []
    #list to hold our uploaded image urls
    filename = session['filename']

    #handle image upload from dropzone
    if request.method == 'POST':
        f = request.files.get('file')
        f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
        #append image urls
        filename.append(filename.url(filename))
        session['filename'] = filename
        return "uploading..."
    return render_template('index.html')


@app.route('/results')
def results():

    #redirect to home if no images to display
    if "filename" not in session or session['filename'] == []:
        return redirect(url_for('index'))

    #set the filename and remove the session variables
    filename = session['filename']
    session.pop('filename', None)
    return render_template('results.html', filename=filename)


if __name__ == '__main__':
    app.run(debug=True)

results.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='index') }}
  {{ dropzone.load_js() }}
  {{ dropzone.config() }}
</body>
</html> ```

0 个答案:

没有答案