2👍
✅
In your editcourse
view, pk
isn’t the kurs
instance, it’s a string with the id (in this case '6'
).
You need to fetch the instance from the db. The shortcut get_object_or_404
is useful for this. Note that you should pass the instance to the form in the GET
and POST
branches of your if statement.
from django.shortcuts import get_object_or_404
def editcourse(request, pk):
kurs = get_object_or_404(Kurs, pk=pk)
if request.method=='POST':
form = KursForm(request.POST, instance=kurs)
if form.is_valid():
form.save()
return HttpResponseRedirect('/polls/usersite')
else:
form = KursForm(instance=kurs)
return render(request, 'polls/editcourse.html', {"form" : form})
Source:stackexchange.com