Explorar el Código

fix url collisions

subDesTagesMitExtraKaese hace 1 año
padre
commit
eccb80a6d6
Se han modificado 1 ficheros con 12 adiciones y 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})