更新旧的 django/twisted python 代码

发布于 2024-11-16 06:13:32 字数 1359 浏览 2 评论 0原文

好吧,我有一些旧的 python 代码似乎无法正常工作,我已经研究了互联网的两端,试图找到修复方法。

def getURL(self, context):
    # Make this an absolute URL- currently it's required for
    # links placed in the RSS and XML feeds, and won't
    # hurt elsewhere.
    req = context['request']                                                                         
    port = req.host[2]
    hostname = req.getRequestHostname()
    if req.isSecure():
        default = 443
    else:
        default = 80
    if port == default:
        hostport = ''
    else:
        hostport = ':%d' % port
    path = posixpath.join('/stats',
                          *(tuple(self.target.pathSegments) + self.relativePathSegments))
    return quote('http%s://%s%s%s' % (
        req.isSecure() and 's' or '',
        hostname,
        hostport,
        path), "/:")

现在我认为这只是 context['request'] 给我带来了问题,但我不确定。 此代码块来自 CIA.vc 项目 (link.py 准确地说),所以如果有些东西没有意义检查

一下我从 python 得到的第一个错误是:

文件“/home/justasic/cia/cia/LibCIA/Web/Stats/Link.py”,第 41 行,在 getURL port = req.host[2] excepts.TypeError: unindexable object

但在我发现我认为是一个简单的修复后,我得到了更多关于未定义 context['request'] 的信息

Well I have some old python code that seems to not work right, I have researched to the ends of the internet trying to find a fix.

def getURL(self, context):
    # Make this an absolute URL- currently it's required for
    # links placed in the RSS and XML feeds, and won't
    # hurt elsewhere.
    req = context['request']                                                                         
    port = req.host[2]
    hostname = req.getRequestHostname()
    if req.isSecure():
        default = 443
    else:
        default = 80
    if port == default:
        hostport = ''
    else:
        hostport = ':%d' % port
    path = posixpath.join('/stats',
                          *(tuple(self.target.pathSegments) + self.relativePathSegments))
    return quote('http%s://%s%s%s' % (
        req.isSecure() and 's' or '',
        hostname,
        hostport,
        path), "/:")

now I think its just the context['request'] giving me issues but I'm not sure.
This code block was from the CIA.vc project (link.py to be exact), so if something doesn't make sense check there

also the 1st error i get from python is:

File "/home/justasic/cia/cia/LibCIA/Web/Stats/Link.py", line 41, in getURL port = req.host[2]
exceptions.TypeError: unindexable object

but I got more about the context['request'] not being defined after I found what I think was a simple fix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2024-11-23 06:13:32

在我看来, Context['request'] 不适合那里...... Context 从哪里来?作为参数,你得到的上下文都是小写的。也许您应该使用参数“context”,所以...

a) 将 Context['request'] 更改为 context['request']

...或者,如果您已经使用小写的上下文,而这只是一个拼写错误在帖子上,然后

b)我搜索了一段时间并找到了这个片段 http://djangosnippets.org/snippets/2428/...所以也许这样的东西可能会起作用:

from django.template import resolve_variable

...

def getURL(self, context):
    req = resolve_variable('request', context)

It seems to me like Context['request'] doesn't fit on there... where does Context come from? As param you get context all lowercase. Probably you sould use the param 'context' instead, so ...

a) make Context['request'] to context['request']

... or, if your are already using context in lowercase, and it's only a typo here on the post, then

b) I searched a while and found this snippet http://djangosnippets.org/snippets/2428/... so maybe something like this might work:

from django.template import resolve_variable

...

def getURL(self, context):
    req = resolve_variable('request', context)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文