有一个注册按钮,可以为该特定事件创建一个参加者。(有效)。但是,单击“注册”后,模板会识别'is_attending'= True
(已注册!)中的每个事件upcoming
。
看法:
def index(request):
upcoming = Event.objects.filter(date__gte=datetime.now())
if request.user.is_authenticated():
profile = Profile.objects.get(user=request.user)
is_attending = False
for event in upcoming:
attendees = [a.profile for a in Attendee.objects.filter(event=event)]
if profile in attendees:
is_attending = True
模板
{% if is_attending %}
<a><Registered!</a>
{% else %}
<form>... registration form ... Register Now...
我想正在发生的事情是:
event 1 : Registered!
event 2 : Registered!
event 3 : Registered!
当我需要的是:
event 1 : Registered!
event 2 : Register Now
event 3 : Register Now
我不太确定为什么这对我不起作用?我如何循环以is_attending
仅返回那些在upcoming
事件中注册的人?
楷模:
class Attendee(models.Model):
event = models.ForeignKey(Event)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
profile = generic.GenericForeignKey('content_type', 'object_id')