Flask脚本返回到根目录,而不是在移动到应用程序后保留在上传页面上



我有一个带有@app.route('/'

当我使用一个简单的Flask脚本时,这绝对很好,但我现在已经将其移动到一个应用程序中,添加了用户身份验证,并将文件上传从/移动到/上传。

我现在遇到的问题是,当文件上传时。我没有在/upload上显示统计数据,而是被重定向回/,我看到了以下内容,我假设在地址栏中获取:

http://192.168.19.180:5000/upload?file_name=uploadedfile.csv

文件仍在上传到static/uploads目录,@main.route中的output_csv_header((函数('/upload'(正在被调用,因为我看到其他进程正在运行。它只是重定向到/而不是停留在/上传我原本希望看到我的统计数据的地方。

我经历了这一切,花了几个小时寻找,结果碰壁了。我想我错过了一些明显的路线,请给我一些建议。

我省略了modules.py中的所有函数,因为它们只是处理数据。

谢谢你的帮助。

#  Displays home page
@main.route('/')
@login_required
def index():
return render_template('index.html')
#  Displays a list of team members
@main.route('/teamlist')
@login_required
def teamlist():
users = User.query.all()
return render_template('teamlist.html', users = users)
#  The route that takes the POST and uploads the file
#  The file is still being uploaded but it's redirecting to / instead
@main.route('/upload', methods=['POST'])
@login_required
def uploadfiles():
""" Upload csv file """
app = current_app._get_current_object()
uploaded_file = request.files['file']
file_name = secure_filename(uploaded_file.filename)
if file_name != '':
file_ext = os.path.splitext(file_name)[1]
if file_ext not in app.config['UPLOAD_EXTENSIONS']:
abort(400)
uploaded_file.save(os.path.join(app.config['UPLOAD_PATH'], file_name))
return redirect(url_for('main.upload', file_name=file_name))

#  If files present in uploads dir it runs output_csv_header func
#  If not it displays a message and the upload form
@main.route('/upload')
def upload():
# Test if there are any files present in the uploads directory
form = FileUpload()
app = current_app._get_current_object()
#if os.listdir(app.config['UPLOAD_PATH']):
if os.listdir('app/static/uploads'):
#files = os.listdir(app.config['UPLOAD_PATH'])
files = os.listdir('app/static/uploads')
csv_file_to_read = files[0]
return output_csv_header(csv_file_to_read)
else:
message = Markup("<i>No Files present, please upload a Response Summary csv file</i>")
return render_template('upload.html', file=message, form=form)

只返回render_template('upload.html', file=message, form=form),而不是POST方法中的重定向。

最新更新