Reputation: 1529
I have created a custom template tag in 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)
and i am loading the same in my template questionDetail.html as
{% load disqusTag %}
{% disqus_sso %}
but i am getting this error : 'str' object does not support item assignment
can anybody help me why? I know similar questions have been asked on stack overflow, but i went through all of them and none of them helped.
Upvotes: 1
Views: 930
Reputation: 599470
You should have provided the full traceback.
However, I think the issue is with your if user.is_anonymous
check - if it's true, you return an empty string. But the return value of an inclusion tag must always be a context dict. You should return an empty dict there instead.
Upvotes: 2