0
执行检查以查看用户是否参加。如何将上下文变量is_attending
传递给模板而不会在'is_attending': context['is_attending']
上发生语法错误?该检查基本上是为造型div和whatnot。我究竟做错了什么?通过包含标签传递上下文变量
模板:
{% for event in upcoming %}
{% registration %}
{% if is_attending %}
Registered!
{% else %}
Register button
{% endif %}
yadda yadda divs...
{% endfor %}
filters.py
@register.inclusion_tag('events/list.html', takes_context=True)
def registration(context, event):
request = context['request']
profile = Profile.objects.get(user=request.user)
attendees = [a.profile for a in Attendee.objects.filter(event=event)]
if profile in attendees:
'is_attending': context['is_attending']
return is_attending
else:
return ''
谢谢!
谢谢您的建议。但是,由于{%registration event%}'模板语法错误,我仍然无法传递'is_attending'。我没有正确调用它吗? – Modelesq
@Modelesq:为您更新了答案; 'take_context'包含标记方法只需要一个'context'参数。 –
模板是事件列表。通过事件循环,必须进行检查。由于这是通过特定事件中的与会者列表筛选的。我需要'{%registration event%}',否则它不会过滤'Attendee.objects.filter(event = event)'并执行检查。 – Modelesq