找不到Flask 404 Url

时间:2017-06-16 00:13:55

标签: python flask

我有一个Flask服务器,如:

from flask import Flask
app = Flask(__name__, static_url_path='')

@app.route('/')
def server_index():
    return app.send_static_file('ngrok_run_a_public_server.html')


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

我将它放在如下文件夹中:

examples
  -ngrok_run_a_public_server.html
  -server.py

它们都在同一个文件夹中,这就是我看到其他人提供静态文件的方式。我在本地和在ngrok上获得404。我只是想公开服务一个文件。

它无法同时使用http://127.0.0.1:5000/http://127.0.0.1:5000

2 个答案:

答案 0 :(得分:1)

只需在examples文件夹中创建一个名为static的文件夹,然后将html文件移到那里。它应该工作。

答案 1 :(得分:1)

你可以在根目录中创建一个文件夹,你想要的任何名字 - 让我们说' myDailyReports '。此文件夹中的文件可以说是“ Day1.xlsx ”。

import os
from flask import send_from_directory, current_app

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

    path = os.path.join(current_app.root_path, 'myDailyReports')
    filename = 'Day1.xlsx'

    # returns the file to download as an attachment with as_attachment as True
    return send_from_directory(directory=path, filename=filename, as_attachment=True)

如果您不想下载,只想在浏览器上显示文件内容,可能是.txt.html文件 -

return send_from_directory(directory=path, filename=filename)
相关问题