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