且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Django中获取整个当前URL

更新时间:2023-02-21 15:16:16

哈希(#)后面的部分是

The part after the hash (#) is the fragment identifier [wiki]. As is specified in the Wikipedia article:

片段标识符的功能与URI的其余部分不同:其片段处理完全是客户端,尽管Web服务器通常有助于确定MIME类型,但是服务器通常不参与Web服务器的参与.MIME类型确定片段的处理.当代理(例如Web浏览器)从Web服务器请求Web资源时,代理将URI发送到服务器,但不发送片段.相反,代理程序等待服务器发送资源,然后代理程序根据文档类型和片段值来处理资源.

The fragment identifier functions differently to the rest of the URI: its processing is exclusively client-sided with no participation from the web server, though the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent (such as a web browser) requests a web resource from a web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.

因此,网络服务器甚至都不会获得片段标识符.如果要在查询字符串的关键字中包含哈希,则需要使用

So the webserver will never even obtain the fragment identifier. If you want to include a hash as part of the key of a querystring, you need to encode it with the percent-encoding [wiki], a hash is encoded to %23. Indeed, if you want the hash to be in the querydict, the URI should be:

的https://本地主机:8000/ 23% =的access_token&EAAJ0Dqh2BJ0BABNKWfkqmiIr3uWwKvpkVeCAVQTZBQSEFG87GKjXunsoofixxKS11ZCicElsZBRMKHL4Dk5nGeBa5lBkYvzw3YKrzZAyZAvhlvd1pAtxzZBPlD4PJaD7JFz4UCjOEIyo5ZCfyBIysva1PCK0XZAN7FpXCDRpDxlEVxtnN9RrbZAt26ZChHV3LRupoZD放大器; data_access_expiration_time = 1576926047&安培; expires_in = 7153 代码>

https://localhost:8000/?%23access_token=EAAJ0Dqh2BJ0BABNKWfkqmiIr3uWwKvpkVeCAVQTZBQSEFG87GKjXunsoofixxKS11ZCicElsZBRMKHL4Dk5nGeBa5lBkYvzw3YKrzZAyZAvhlvd1pAtxzZBPlD4PJaD7JFz4UCjOEIyo5ZCfyBIysva1PCK0XZAN7FpXCDRpDxlEVxtnN9RrbZAt26ZChHV3LRupoZD&data_access_expiration_time=1576926047&expires_in=7153

如果我们生成此类URI,则Django会将其解析为:

If we generate such URI, then Django will parse this as:

>>> QueryDict('%23access_token=EAAJ0Dqh2BJ0BABNKWfkqmiIr3uWwKvpkVeCAVQTZBQSEFG87GKjXunsoofixxKS11ZCicElsZBRMKHL4Dk5nGeBa5lBkYvzw3YKrzZAyZAvhlvd1pAtxzZBPlD4PJaD7JFz4UCjOEIyo5ZCfyBIysva1PCK0XZAN7FpXCDRpDxlEVxtnN9RrbZAt26ZChHV3LRupoZD&data_access_expiration_time=1576926047&expires_in=7153')
<QueryDict: {'#access_token': ['EAAJ0Dqh2BJ0BABNKWfkqmiIr3uWwKvpkVeCAVQTZBQSEFG87GKjXunsoofixxKS11ZCicElsZBRMKHL4Dk5nGeBa5lBkYvzw3YKrzZAyZAvhlvd1pAtxzZBPlD4PJaD7JFz4UCjOEIyo5ZCfyBIysva1PCK0XZAN7FpXCDRpDxlEVxtnN9RrbZAt26ZChHV3LRupoZD'], 'data_access_expiration_time': ['1576926047'], 'expires_in': ['7153']}>

这是为什么您永远不要自己进行字符串处理来编码查询字符串的主要原因之一.当它们是键或值的一部分时,应该编码更多的字符,例如 * '(); : @ & = + $ /? [] .尽管严格来说,您可以自己对它们进行编码.使用经过有效测试的工具可能更安全.

This is one of the main reasons why you should never do string processing yourself to encode a querystring. There are more characters that should be encoded when these are part of a key or value, like !, *, ', (, ), ;, :, @, &, =, +, $, ,, /, ?, [ and ]. Although strictly speaking, you can encode these yourself. It is likely more safe to use a tool that has been tested effectively.