from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render, get_object_or_404 from django.urls import reverse from .models import Event, EventRegistration def index(request): events = Event.objects.order_by("-start_year")[:20] events_data = [ { "event": event, "my_registration": event.registration_of(request.user), } for event in events ] return render(request, "events/index.html", {"events": events_data}) def register(request, event_id): if request.method != "POST": return HttpResponse(status_code=405) event = get_object_or_404(Event, id=event_id) event.eventregistration_set.create( event=event, user=request.user, ) return HttpResponseRedirect(reverse("events:index") + f"#{event.id}") def deregister(request, event_id): if request.method != "POST": return HttpResponse(status_code=405) registration = get_object_or_404( EventRegistration, event=event_id, user=request.user ) registration.delete() return HttpResponseRedirect(reverse("events:index") + f"#{event_id}")