更新旧的Django/twisted python cod

2024-09-28 01:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有一些旧的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 project(确切地说是link.py),因此如果有什么不合理的地方,请检查那里

另外,我从python得到的第一个错误是:

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

但是在我找到一个简单的修复程序之后,我得到了更多关于context['request']没有被定义的信息


Tags: and代码inselfdefaulthostifport
1条回答
网友
1楼 · 发布于 2024-09-28 01:25:37

在我看来上下文['request']不适合这里。。。语境从何而来?作为param,上下文都是小写的。也许你应该改用参数'context',所以。。。在

a)将上下文['request']转换为Context['request']

。。。或者,如果你已经在使用小写的上下文,而这只是帖子上的一个打字错误,那么

b)我搜索了一段时间,找到了这个片段http://djangosnippets.org/snippets/2428/。。。所以也许这样的方法可能有用:

from django.template import resolve_variable

...

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

相关问题 更多 >

    热门问题