1👍
✅
You can not make project
the instance: your form works on an Event
. By using instance=project
you let the form edit attributes that belong to the event
like event_name
, but these will not be saved to the database, since a Project
has no column event_name
.
The view thus should take request.POST
as data, and we set the .project
of the Event
wrapped in the form with:
from django.shortcuts import get_object_or_404
@login_required
def AddEvent(request,project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == 'POST':
form = addEventForm(request.POST)
if form.is_valid():
form.instance.project = project
form.save()
return redirect('http://127.0.0.1:8000/')
else:
form = addEventForm()
return render(request, 'pages/add_event.html', {'project': project, "form": form})
Source:stackexchange.com