为什么在Python / Flask中包含sharp(#)符号的非现有URL不是由错误处理程序处理的 [英] Why non existing URL containing the sharp ("#") sign in Python/Flask aren't handled by the errorhandler

查看:258
本文介绍了为什么在Python / Flask中包含sharp(#)符号的非现有URL不是由错误处理程序处理的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Python / Flask(完全是ajax,没有整页重装)web应用程序,当url上的锐利(#)符号变为(本Alman,HashChange事件)



喜欢抓住所有Error-404在我的母版页追加错误页面

考虑以下代码



<
$ from flask import烧瓶,重定向
$ b $ #WebServerGatewayInterface $ b $ class WSGI():
def __init __(self):
self.app = Flask(__ name__)
self.app.debug = True


wsgi = WSGI()

@ wsgi.app.route (/)
def Home():
返回Home

@ wsgi.app.route(/ Contact)
def Contact() :
返回Contact

@ wsgi.app.errorhandler(404)
def PageNotFound(错误):
返回PageNotFound

if __name__ =='__main__':
wsgi.app.run(host ='127.0.0.1',port = 7777)

我想知道为什么当我在sharp(#)符号之后添加一个不存在的URL时,它不会被错误处理程序捕获到?



  127.0.0.1:7777 
#返回Home

127.0.0.1 :7777 /联系
#返回联系

127.0.0.1:7777/Aasdasdasdasd
#返回PageNotFound

127.0.0.1 :7777 /#/ asdsaasdasdasdas
#它返回Home,这是不正确的,因为这个URL不存在

127.0.0.1:7777/#!/asdsaasdasdasdas
#返回家,不好...

127.0.0.1:7777/#!/Contact
#它返回家,不好...


解决方案

c>形成资源的片段标识符,并在客户端 。在正常的操作过程中,片段标识符永远不会发送到服务器:


片段标识符的功能与URI的其余部分不同:即,它的处理完全是客户端,没有来自web服务器的参与。

如果一个服务器发送了一个片段标识符一个URL,它必须忽略这部分的URL。这不是路径的一部分,因此,对于所有带有片段的示例URL,Flask只会看到 127.0.0.1:7777 /


I'm building a Python/Flask (fully ajax, without full page reloading) web application and I change the content of my master page when the sharp ("#") sign on the url change (Ben Alman, HashChange event)

I would like to catch all Error-404 to append an error page in my master page

Taking the following code in consideration

from flask import Flask, redirect

#WebServerGatewayInterface
class WSGI():
    def __init__(self):
        self.app = Flask(__name__)
        self.app.debug = True


wsgi = WSGI()

@wsgi.app.route("/")
def Home():
    return "Home"

@wsgi.app.route("/Contact")
def Contact():
    return "Contact"

@wsgi.app.errorhandler(404)
def PageNotFound(error):
    return "PageNotFound"

if __name__ == '__main__':
    wsgi.app.run(host='127.0.0.1', port=7777)

I would like to understand why when I add a non-exising URL after the sharp ("#") sign, it isn't trapped by the errorhandler ?

E.g.

127.0.0.1:7777
#It returns "Home"

127.0.0.1:7777/Contact
#It returns "Contact"

127.0.0.1:7777/Aasdasdasdasd
#It returns "PageNotFound"

127.0.0.1:7777/#/asdsaasdasdasdas
#It returns "Home", which is not right since this URL doesn't exist

127.0.0.1:7777/#!/asdsaasdasdasdas
#It returns "Home", not ok...

127.0.0.1:7777/#!/Contact
#It returns "Home", not ok...

解决方案

Everything after the # forms the fragment identifier of a resource and is handled client side. In the normal course of operation, the fragment identifier is never sent to the server:

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server

If a server is ever sent a fragment identifier in a URL, it must ignore this part of the URL. It is not part of the path, and thus Flask just sees 127.0.0.1:7777/ for all your sample URLs with a fragment.

这篇关于为什么在Python / Flask中包含sharp(#)符号的非现有URL不是由错误处理程序处理的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆