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から取得する最初のエラーは次のとおりです。

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

context['request']しかし、簡単な修正だと思うものを見つけた後、定義されていないことについてもっと知りました

4

1 に答える 1

0

Context ['request']がそこに収まらないように私には思えます...Contextはどこから来たのですか?paramとして、コンテキストはすべて小文字で取得します。おそらく、代わりにparam'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)
于 2011-06-18T23:51:22.963 に答える