Python Flask静态文件404 main.py文件中的代码:
当我转到根目录或Python Flask静态文件404 main.py文件中的代码:,python,nginx,flask,Python,Nginx,Flask,当我转到根目录或/about目录时,它工作正常,但当我尝试转到/projects目录时,我得到了错误: 错误消息: 找不到 在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试 有两个可能的原因 你打错了路径。您是否在projects(即project)或index.html)中有打字错误 路径不存在。与render_template不同,app.send_static_file如果路径不存在,则会死掉。由于static\u文件夹是static,因此项目页面代码应该存在于stat
/about
目录时,它工作正常,但当我尝试转到/projects
目录时,我得到了错误:
错误消息:
找不到
在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试
有两个可能的原因
projects
(即project
)或index.html
)中有打字错误render_template
不同,app.send_static_file
如果路径不存在,则会死掉。由于static\u文件夹
是static
,因此项目页面代码应该存在于static/projects/index.html
下(而不是projects/index.html
)返回“some string”
。如果那根绳子不露出来,你的手上就有另一头野兽。如果是的话,那么它肯定是我在上面识别的两个bug之一
在一个不相关的注释中,我将把
debug=True
添加到app.run(…)
kwargs的列表中,以使开发更加方便。(只要保存文件,应用程序就会刷新)是否存在projects/index.html
文件?它可读吗?请尝试从flask目录中打印python-c'print(open(“projects/index.html”).read()”
,看看它是否打印出您所期望的内容。@ChristianTernus是的,我这样做时它打印得很好您可能在[snip]
中的某个地方有另一个项目的定义吗?@ChristianTernus nope,如果你愿意的话,我可以给你文件的其余部分,但我不认为是这样,路径肯定在那里。它只在本地机器上工作,但当我将文件复制到服务器上时,除了项目之外,其他一切都正常工作。页面的目录是/static/projects/index.html。当我试图返回一个字符串时,它不会显示,所以可能就是这样。我该怎么解决呢?我发现了我的问题。当我输入[website]/projects
时,它会自动将其转换为[website]/projects/
,然后返回404错误。我不知道为什么会发生这种情况,但我通过将@app.route(“/projects”)
更改为@app.route(“/projects/”)
from flask import Flask, render_template, send_from_directory
app = Flask(__name__, static_url_path="")
app._static_folder = "static"
@app.route("/")
def root():
return app.send_static_file("index.html")
@app.route("/about")
def about():
return app.send_static_file("about/index.html")
@app.route("/projects")
def projects():
return app.send_static_file("projects/index.html")
#snip
if __name__ == "__main__":
app.run(host="0.0.0.0")