Python 为什么即使链接在服务器上,我也会收到“404 Not Found”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21807865/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get a "404 Not Found" error even though the link is on the server?
提问by user3047960
I'm running a simple test site on PythonAnywhereusing Flask. When I run the script, the initial site (index.html) appears, and everything seems fine. However, when I click on any of the links (like signup.html), I get a 404 error:
我正在使用 Flask在PythonAnywhere上运行一个简单的测试站点。当我运行脚本时,初始站点 (index.html) 出现,一切看起来都很好。但是,当我单击任何链接(如 signup.html)时,我收到 404 错误:
Not Found
The requested URL was not found on the server.
If you entered the URL manually please check your spelling and try again.
Not Found
在服务器上找不到请求的 URL。
如果您手动输入了 URL,请检查您的拼写并重试。
However, the HTML files are all in the templates folder, along with index.html. Why can't they be found on the server?
但是,HTML 文件和 index.html 都在模板文件夹中。为什么在服务器上找不到?
Here is the Python code that runs the app:
这是运行该应用程序的 Python 代码:
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def runit():
    return render_template('index.html')
if __name__ == '__main__':
    app.run()
And here is the HTML portion of index.html that holds the link:
这是 index.html 的 HTML 部分,其中包含链接:
<a class="btn btn-lg btn-success" href="signup.html">Sign up</a>
采纳答案by Jacinda
You need to create another route for your signup URL, so your main webapp code needs to add a route for '/signup.html', i.e.
您需要为您的注册 URL 创建另一个路由,因此您的主 webapp 代码需要为“/signup.html”添加一个路由,即
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def runit():
    return render_template('index.html')
@app.route('/signup.html')
def signup():
    return render_template('signup.html')
if __name__ == '__main__':
    app.run()
If you want your URLs to be a little cleaner, you can do something like this in your Python:
如果你想让你的 URL 更简洁一些,你可以在你的 Python 中做这样的事情:
@app.route('/signup')
def signup():
    return render_template('signup.html')
And change your link code to match.
并更改您的链接代码以匹配。
<a class="btn btn-lg btn-success" href="signup">Sign up</a>
The main Flask documentation has a good overview of routes in their Quickstart guide: http://flask.pocoo.org/docs/quickstart/
Flask 的主要文档在他们的快速入门指南中有一个很好的路由概述:http: //flask.pocoo.org/docs/quickstart/

