There’s a registration button that creates an Attendee for that specific event.(that works). However, upon clicking ‘Register’, the template recognizes 'is_attending'= True(Registered!) for every Event in upcoming.
View:
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
template
{% if is_attending %}
<a><Registered!</a>
{% else %}
<form>... registration form ... Register Now...
I guess what’s happening is:
event 1 : Registered!
event 2 : Registered!
event 3 : Registered!
When what I need is :
event 1 : Registered!
event 2 : Register Now
event 3 : Register Now
I’m not quite sure why this isn’t working for me? How do I loop through to return is_attending for only those who have registered within upcoming events?
Models:
class Attendee(models.Model):
event = models.ForeignKey(Event)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
profile = generic.GenericForeignKey('content_type', 'object_id')
You’re over-writing the value of
is_attendingevery time you go through the for loop; your template sees the (single) value ofis_attendingas set by whether the last event inupcomingis being attended byrequest.user.To get the desired behaviour, one piece of
is_attendinginfo for eachEvent. I think the easiest way to do this is with a custom template tag:Then, while you’re looping over your events in the template:
You could also consider using an assignment tag or an inclusion tag depending on your needs.