'is_attending': context['is_attending']
is not valid python. Rather, it looks like a partial dictionary. Since .inclusion_tag()
code is supposed to return a dict, perhaps you meant the following instead:
if profile in attendees:
return {'is_attending': context['is_attending']}
else:
return {'is_attending': ''}
Also note that takes_context
means you'll only take the context as an argument. From the howto on custom tags:
If you specify takes_context in creating a template tag, the tag will have no required arguments, and the underlying Python function will have one argument -- the template context as of when the tag was called.
Thus your tag should be:
{% registration %}
and your full method can take the event
argument directly from the context:
@register.inclusion_tag('events/list.html', takes_context=True)
def registration(context):
request = context['request']
event = context['event']
profile = Profile.objects.get(user=request.user)
attendees = [a.profile for a in Attendee.objects.filter(event=event)]
if profile in attendees:
return {'is_attending': context['is_attending']}
else:
return {'is_attending': ''}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…