如何在Django中获取整个当前URL [英] how to get entire current url in django

查看:45
本文介绍了如何在Django中获取整个当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中获取当前网址有些麻烦.当前URL是的https://本地主机:8000/#=的access_tokenEAAJ0Dqh2BJ0BABNKWfkqmiIr3uWwKvpkVeCAVQTZBQSEFG87GKjXunsoofixxKS11ZCicElsZBRMKHL4Dk5nGeBa5lBkYvzw3YKrzZAyZAvhlvd1pAtxzZBPlD4PJaD7JFz4UCjOEIyo5ZCfyBIysva1PCK0XZAN7FpXCDRpDxlEVxtnN9RrbZAt26ZChHV3LRupoZD&安培; data_access_expiration_time = 1576926047&安培; expires_in = 7153 '

I have some trouble getting current url in django. current url is 'https://localhost:8000/?#access_token=EAAJ0Dqh2BJ0BABNKWfkqmiIr3uWwKvpkVeCAVQTZBQSEFG87GKjXunsoofixxKS11ZCicElsZBRMKHL4Dk5nGeBa5lBkYvzw3YKrzZAyZAvhlvd1pAtxzZBPlD4PJaD7JFz4UCjOEIyo5ZCfyBIysva1PCK0XZAN7FpXCDRpDxlEVxtnN9RrbZAt26ZChHV3LRupoZD&data_access_expiration_time=1576926047&expires_in=7153'

我使用了几种URL获取功能,例如request.get_full_path()和request.build_absolute_uri(),但它们只返回了' https://localhost:8000 '

i used several url getting functions such as request.get_full_path() and request.build_absolute_uri(), but they did only returns 'https://localhost:8000'

如何获取'#'之后的字符串,例如access_token?

How can i get the string following '#' such as access_token?

推荐答案

哈希(#)后面的部分是

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.

这篇关于如何在Django中获取整个当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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