问题描述
限时送ChatGPT账号..我没有使用os.environ
,但这可能是最简单的方法.
I don't have to use os.environ
but that might be the easiest method.
我可以使用以下方法成功获取上一个 URL:
I can successfully get the previous URL using:
sURL = os.getenv('HTTP_REFERER')
和当前字符串的参数使用:
and the arguments of the current string using:
sURL = os.environ['QUERY_STRING']
但我无法获取当前 URL.
but I cannot get the current URL.
这里有其他答案建议:
sURL = self.request.url
(或 self.request.host) 但是当我这样做时,我得到:
(or self.request.host) but when I do that I get:
sURL = self.request.url NameError: global name 'self' is not defined
如何轻松获取当前 URL?
How can I easily get the current URL?
完整代码:
import os
def GetTestMode ():
sURL = self.request.url
print '<br />sURL = ' + sURL + '<br />'
推荐答案
感谢一位同事,这可以通过以下方式解决:
Thanks to a colleague, this can be solved with:
import os.path
sURL = os.path.realpath('.')
print 'sURL = ' + sURL + '<br />'
请参阅 os.path 说,返回规范指定文件名的路径,消除路径中遇到的任何符号链接(如果操作系统支持它们)."
所以这不会返回 URL 而是文件路径,但我也可以使用它.
So this does not return the URL but the file path, but I can also use this.
这篇关于使用 os.environ 在 Python 中获取当前 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论