1👍
✅
You are only creating variables word1
, word2
, etc. when the form is valid, but you use these variables in the context
, even if these ar enot valid hence the error.
You thus should assign something to word1
, word2
, word3
and result
in case the form is not valid, for example:
def index(request):
form = WordForm(request.POST)
if form.is_valid():
word1 = form.cleaned_data['Word1']
word2 = form.cleaned_data['Word2']
word3 = form.cleaned_data['Word3']
result = form.cleaned_data['Result']
else:
word1 = word2 = word3 = result = None
context = {'form': form, 'word1': word1, 'word2': word2, 'word3': word3, 'result': result}
return render(request, 'form.html', context)
Source:stackexchange.com