Explorar o código

fix url collisions

subDesTagesMitExtraKaese hai 1 ano
pai
achega
eccb80a6d6
Modificáronse 1 ficheiros con 12 adicións e 6 borrados
  1. 12 6
      zitap/views.py

+ 12 - 6
zitap/views.py

@@ -20,12 +20,18 @@ def create_event(request):
         form = CreateEventForm(request.POST)
         if form.is_valid():
             data = form.cleaned_data
-            print(data)
-            event = Event.objects.create(name=data['event_name'])
-            for date in data['event_date']:
-                event.date_set.create(date=date)
-            event.save()
-            return HttpResponseRedirect(f'/{event.url}')
+            # Try to create the event. If the url is already taken, try again.
+            n = 0
+            while n<10:
+                try:
+                    event = Event.objects.create(name=data['event_name'])
+                    for date in data['event_date']:
+                        event.date_set.create(date=date)
+                    event.save()
+                    return HttpResponseRedirect(f'/{event.url}')
+                except IntegrityError:
+                    n += 1
+            form.add_error(None, 'Could not create event. Please try again.')
     else:
         form = CreateEventForm()
     return render(request, 'zitap/create-event.html', {'form': form})