简体   繁体   English

更新旧的django / twisted python代码

[英]Updating old django/twisted python code

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. 好吧,我有一些旧的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), "/:")

now I think its just the context['request'] giving me issues but I'm not sure. 现在我认为这只是context['request']给我的问题,但我不确定。 This code block was from the CIA.vc project ( link.py to be exact), so if something doesn't make sense check there 该代码块来自CIA.vc项目确切地说link.py ),因此如果没有任何意义,请在此处检查

also the 1st error i get from python is: 我从python得到的第一个错误是:

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 但是当我发现我认为是简单的解决方法之后,我对未定义context['request']有了更多了解

It seems to me like Context['request'] doesn't fit on there... where does Context come from? 在我看来,Context ['request']不适合在那里... Context来自何处? As param you get context all lowercase. 作为参数,您将上下文全部小写。 Probably you sould use the param 'context' instead, so ... 可能是您改用了参数“上下文”,所以...

a) make Context['request'] to context['request'] a)使Context ['request']成为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: b)我搜索了一会儿,发现此代码段http://djangosnippets.org/snippets/2428/ ...因此,也许这样的代码可能有效:

from django.template import resolve_variable

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM