理解Flask中的路由/ URL映射。

huangapple go评论67阅读模式
英文:

Understanding of routing/mapping URL in Flask

问题

我一直在学习Flask,参考了这个教程。在完成所有步骤后,我决定修改一些代码,以便了解其中的bug和工作原理。我遇到了理解这个函数背后的过程(可能是路由)的问题:

from app import app

@app.route('/')
@app.route('/index')
def index():
    return "干杯"

我尝试将它粘贴到搜索引擎中:

http://localhost:5000/index

结果是在屏幕上显示了一条消息。然后我将代码更改为:

from package import app

@app.route('/wiki')
def got_it():
    return "现在你搞砸了"

并在维基百科页面上得到了错误。我键入了:

en.wikpedia.org/wiki/Messed_up

我对Flask非常新手,但我想请教您进一步的信息或解释,为什么更改URL规则没有起作用。这是维基百科网站的加密问题,还是应用程序需要IP地址才能工作的问题?我在几个网站上都读过有关路由的资料,但是似乎没有找到解释。

快速编辑:

  • 维基百科错误:维基百科上没有这篇文章 - 当您在模板中输入错误的地址时,通常会出现此消息:en.wikpedia.org/wiki/Article
  • 维基百科只是一个示例 - 第一个闪现在我脑海中的网站。
  • 我想检查所有URL是否都适用于Flask应用程序,或者路由是否与特殊情况相关,例如 - 没有加密

亲爱的问题贡献者们:

我尝试了marxmacher的建议:

http://localhost:5000/wiki

结果是在屏幕上显示了一条消息。因此 - 它起作用了。但是,我想知道,为什么Flask应用程序在这种情况下需要localhost,而在维基百科模板(en.wikipedia.org/wiki/)上无法工作呢?

英文:

I have been working on this tutorial in order to learn Flask. After accomplishing all steps, I decided to modify some code so as to learn on bugs 理解Flask中的路由/ URL映射。 how everything works. I have the problem with understanding of the process (probably routing) behind this function:

from app import app

@app.route('/')
@app.route('/index')
def index():
     return "Cheers"

which I have tried, pasting to search engine:

> http://localhost:5000/index

The result - the message on a screen. Then I have changed the code to:

from package import app

@app.route('/wiki')
def got_it():
     return "Now you messed up"

and got the error on Wikipedia page. I have typed:

> en.wikpedia.org/wiki/Messed_up

I am very new to Flask, nevertheless I would like to ask you for further information or explanation, why the change of the URL rule didn't work. Is it the matter of encryption of Wikipedia website or does an app need the IP address to work? I have read about routing on several websites, however - supposedly - I didn't notice the explanation.

Quick edit:

  • Wikipedia error: no such an article on Wikipedia - typical message, when you type wrong address in the template: en.wikpedia.org/wiki/Article

  • Wikipedia is only an example - the first website which has crossed my mind

  • I wanted to checkwhether all URLs could work for flask app or whether routing is connected to special cases, for example - without encryption

Dear All Contributors to the question:

I have tried marxmacher's suggestion:

> http://localhost:5000/wiki

which resulted in the message on a screen. Thus - it worked. However, I would like to know, why did Flask app need localhost in this case and didn't work on Wikipedia template (en.wikipedia.org/wiki/)?

答案1

得分: 1

为什么这个有效:

http://localhost:5000/wiki

您的Flask应用当前正在本地计算机上运行。它将无法在en.wikipedia.org/wiki上运行,因为wikipedia.org是由其他人拥有的域名。

如果您希望您的Flask应用程序对公众可见,您需要一个公共IP,并将您的Flask应用程序指向那里,这样前来访问您的公共IP的人就可以由您的Flask应用程序提供服务。

英文:

Why this worked:

http://localhost:5000/wiki

Your Flask App is running on your local machine right now. It will not work for en.wikipedia.org/wiki since wikipedia.org is a domain name owned by someone other than you.

If you want your Flask app to be publicly visible you need a public IP and point your Flask app there so people coming to your public IP can be served by your Flask app.

huangapple
  • 本文由 发表于 2020年1月3日 20:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定