subDesTagesMitExtraKaese 1 год назад
Родитель
Сommit
eccb80a6d6
1 измененных файлов с 12 добавлено и 6 удалено
  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})