I'm trying to make a Django application to handle events. The view below handles the editing of already created events.
@login_required
def event_admin(request, event_id):
event = get_object_or_404(Event, pk=event_id)
if request.method == 'POST' and request.user == event.organiser:
event_form = EventAdminForm(request.POST, instance=event)
if event_form.is_valid():
event_form.save()
return HttpResponseRedirect(reverse('event_event_detail',
args=(event.id, )))
elif request.user == event.organiser:
event_form = EventAdminForm(instance=event)
return render_to_response('events/event_admin.html',
{'event_form': event_form, 'event': event},
context_instance = RequestContext(request))
return HttpResponseRedirect(reverse('event_event_detail', args=(event.id, )))
When an existing event is edited the existing event is left untouched while a new event is created with the changes that were made. Can someone see what I am doing wrong? Many thanks.
Edit:
Here is the relevant section of forms.py if it helps.
class EventAdminForm(ModelForm):
start_time = forms.DateTimeField(widget=SplitSelectDateTimeWidget(
minute_step=5, second_step=60, years=range(2009, 2015)),
initial=datetime.datetime.now())
end_time = forms.DateTimeField(widget=SplitSelectDateTimeWidget(
minute_step=5, second_step=60, years=range(2009, 2015)),
initial=datetime.datetime.now())
def clean(self):
cleaned_data = self.cleaned_data
start_time = cleaned_data.get('start_time')
end_time = cleaned_data.get('end_time')
# The start of an event cannot be after the end.
if start_time > end_time:
raise forms.ValidationError(u'The end of an event can not be before the start.')
return cleaned_data
class Meta:
model = Event
exclude = ('attendees', 'organiser', )