【发布时间】:2015-07-28 14:51:00
【问题描述】:
我在 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”对象不支持项目分配
任何人都可以帮助我为什么?我知道在堆栈溢出问题上也有人问过类似的问题,但我都解决了所有问题,但没有一个有帮助。
【问题讨论】:
-
这个错误出现在哪一行代码?