Python 无法使用烧瓶路由到“/登录”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14956656/
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
Can't route to "/login" with flask?
提问by winoi
When I type /loginas url,it will go wrong
当我输入/login网址时,它会出错
For example:
例如:
from flask import Flask ,url_for,render_template,request
app = Flask(__name__)
@app.route('/login')
def index():
return "index"
if __name__== "__main__":
app.run()
The error turn out to be like this:
错误结果是这样的:
Not Found.
The requested URL was not found on the server.
When I replace /loginwith /login/or any other words like /log, it will be all right. How does that happen?
当我替换/login为/login/或任何其他类似的词时/log,就可以了。怎么会这样?
采纳答案by Joe
Please read the flask quickstartUnique URLs / Redirection Behavior, URL canonicalizationand Trailing slash in URLs - which style is preferred?
请阅读Flask 快速入门Unique URLs / Redirection Behavior, URL canonicalizationand Trailing slash in URLs - 首选哪种样式?
回答by rox
EDIT
编辑
You could turn off the strict url modein the route module, to get the /login/request working
您可以关闭路由模块中的严格 url 模式,以使/login/请求正常工作
Add the following code after you app = Flask(__name__)and before you define any routing.
在您之后app = Flask(__name__)和定义任何路由之前添加以下代码 。
app.url_map.strict_slashes = False
Original Answer
原答案
My chrome is messing the request somehow. I open the <F12>developer tools and find that it automatically redirect my /loginrequest to /login/.
我的 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
This is awkward. I don't know how to fix this issue. I guess the best solution is to use /login/style instead.
这很尴尬。我不知道如何解决这个问题。我想最好的解决方案是改用/login/样式。

