|
@@ -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})
|