from django.shortcuts import render, redirect from django.urls import reverse from .models import InvitedGuest def index(request): return render(request, 'index.html') def try_again(request): return render(request, 'index.html', {'invalid_code': True}) def rsvp(request): rsvpCode = request.GET['rsvp-code'] guests = InvitedGuest.objects.filter(rsvpCode = rsvpCode) if request.method == 'GET': if not guests: # No guests with that code return redirect(reverse('try-again') + '#rsvp') return render (request, 'rsvp.html', {'guests':guests, 'rsvpCode': rsvpCode}) elif request.method == 'POST': any_attending = False for guest in guests: attending = request.POST['attending_%d' % guest.id] == 'attending' guest.attending = attending any_attending = any_attending or attending guest.save() if any_attending: return redirect('thanks') else: return redirect('sorry') def thanks(request): return render(request, 'thank-you.html', {'attending': True}) def sorry(request): return render(request, 'thank-you.html', {'attending': False})