2015-05-16 89 views
0

我已經在Django「海峽」對象不支持

disqusTag.py: 

register = template.Library() 
@register.inclusion_tag('polls/questionDetail.html', takes_context=True) 
def disqus_sso(context): 
    DISQUS_SECRET_KEY = getattr(settings, 'DISQUS_SECRET_KEY', None) 
    if DISQUS_SECRET_KEY is None: 
     return "<p>You need to set DISQUS_SECRET_KEY before you can use SSO</p>" 

    DISQUS_PUBLIC_KEY = getattr(settings, 'DISQUS_PUBLIC_KEY', None) 
    if DISQUS_PUBLIC_KEY is None: 
     return "<p>You need to set DISQUS_PUBLIC_KEY before you can use SSO</p>" 

    user = context['user'] 

    if user.is_anonymous(): 
     return "" 

    data = json.dumps({ 
     'id': user.id, 
     'username': user.username, 
     'email': user.email, 
    }) 

    # encode the data to base64 
    message = base64.b64encode(data.encode('utf-8')) 

    # generate a timestamp for signing the message 
    timestamp = int(time.time()) 

    key = DISQUS_SECRET_KEY.encode('utf-8') 
    msg = ('%s %s' % (message, timestamp)).encode('utf-8') 
    digestmod = hashlib.sha1 

    # generate our hmac signature 
    sig = hmac.HMAC(key, msg, digestmod).hexdigest() 

    return dict(
     message=message, 
     timestamp=timestamp, 
     sig=sig, 
     pub_key=DISQUS_PUBLIC_KEY, 
    ) 

    t = get_template('polls/questionDetail.html') 
    register.inclusion_tag(t)(disqus_sso) 

和我加載在我的模板questionDetail.html一樣

{% load disqusTag %} 
{% disqus_sso %} 

但我創建一個自定義模板標籤項任務我得到這個錯誤:'str'對象不支持項目分配

任何人都可以幫助我爲什麼?我知道類似的問題已經被問到堆棧溢出問題,但我經歷了所有這些問題,但都沒有幫助。

+0

在哪行代碼出現此錯誤? – Charlesthk

回答

0

你應該提供完整的回溯。

但是,我認爲問題出在您的if user.is_anonymous檢查 - 如果它是真的,您返回一個空字符串。但是,包含標籤的返回值必須始終是上下文字典。您應該返回空字典。

相關問題