1👍
✅
Your calendar/event/new/
route points to EventView
, which only has a get method defined. Posting to a view without a post method will produce a 405 error.
Also, your EditEventView doesn’t edit an event, but instead always creates a new event.
To fix this, you need to do this:
- Copy the post method in
EditEventView
toEventView
. - Change the original post method in
EditEventView
to only edit existing events:
EventEditView
post method:
def post(self, request, event_id):
instance = get_object_or_404(Event, pk=event_id)
form = EventForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('main_website_calendar')
You should also implement some error handling in these post requests, but I’ll leave that to you.
EDIT:
EventView
post method:
def post(self, request):
instance = Event()
form = EventForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('main_website_calendar')
Source:stackexchange.com