The view below is gives me the error when using the POST method. I\'m trying to load the model data into a form, allow the user to edit, and then update the database. When I try
I was with this same error, believe it or not, was the indentation of Python.
your error to the indentation of a Python file. You have to be careful when following tutorials and/or copy-pasting code. Incorrect indentation can waste lot of valuable time.
You are redirecting if form.is_valid()
but what about the form is invalid? There isn't any code that get's executed in this case? There's no code for that. When a function doesn't explicitly return a value, a caller that expects a return value is given None
. Hence the error.
You could try something like this:
def edit(request, row_id):
rating = get_object_or_404(Rating, pk=row_id)
context = {'form': rating}
if request.method == "POST":
form = RatingForm(request.POST)
if form.is_valid():
form.save()
return redirect('home.html')
else :
return render(request, 'ratings/entry_def.html',
{'form': form})
else:
return render(
request,
'ratings/entry_def.html',
context
)
This will result in the form being displayed again to the user and if you have coded your template correctly it will show which fields are invalid.