当我输入/login
作为网址时,它会出错
例如:
from flask import Flask ,url_for,render_template,request
app = Flask(__name__)
@app.route('/login')
def index():
return "index"
if __name__== "__main__":
app.run()
错误结果如下:
Not Found.
The requested URL was not found on the server.
当我将/login
替换为/login/
或任何其他字词/log
时,它就可以了。这是怎么发生的?
答案 0 :(得分:7)
请阅读flask quickstart 唯一网址/重定向行为,URL canonicalization和Trailing slash in URLs - which style is preferred?
答案 1 :(得分:0)
您可以在路由模块中turn off the strict url mode,以使/login/
请求正常工作
在app = Flask(__name__)
之后和定义任何路由之前添加以下代码。
app.url_map.strict_slashes = False
我的Chrome以某种方式弄乱了请求。我打开<F12>
开发人员工具,发现它会自动将我的/login
请求重定向到/login/
。
General
Request URL:http://roxma.org:8000/hello
Request Method:GET
Status Code:301 MOVED PERMANENTLY (from disk cache)
Remote Address:127.0.0.1:1080
Request
Content-Length:263
Content-Type:text/html; charset=utf-8
Date:Wed, 28 Dec 2016 14:24:44 GMT
Location:http://roxma.org:8000/hello/
Server:Werkzeug/0.11.11 Python/3.5.1
这很尴尬。我不知道如何解决这个问题。我想最好的解决方案是使用/login/
样式。