So I have this calendar website which the user can add events to. They can create a new event to add to the calendar or they can edit current events they have already added to the calendar. However, when they edit an event that has already been added and hit the submit button to post it, it creates a whole new event instead of editing the current one. I'm trying to get it to where when they edit an event that has been added, instead of creating a new event on submit, it edits the current one which they clicked on in the first place .

def event(request, event_id=None):

    if event_id:
        instance = get_object_or_404(Event, pk=event_id)
        instance = Event()

    form = EventForm(request.POST or None, instance=instance)

    if request.POST and form.is_valid():
        event = Event.objects.create(**form.cleaned_data, user=request.user)
        return HttpResponseRedirect(reverse('cal:calendar'))
    return render(request, 'cal/event.html', {'form': form})

1 Answers

Daniel Roseman On

You shouldn't use Event.objects.create. EventForm is a ModelForm, since you pass instance to it; so, you can call its save method.

if form.is_valid():
    event =
    return HttpResponseRedirect(reverse('cal:calendar'))