开发者

Updating old django/twisted python code

开发者 https://www.devze.com 2023-03-14 08:18 出处:网络
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.

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']                                                                         
    p开发者_如何学Pythonort = 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


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号