[Answered ]-Django AttributeError, KeyError

2👍

Your render completely ignores the form you’ve provided

return render(request, 'newNote.html', {'note_form': NoteForm, 'registered': registered})

Should be

return render(request, 'newNote.html', {'note_form': form, 'registered': registered})

This should solve the original key error issue you were having, the issue you found when changing the code would need different error handling

self.user = kwargs.pop('user', None)
# self.validate = kwargs.pop('validate', False)
super(NoteForm, self).__init__(*args, **kwargs)

if self.user is not None:
    groups = StudentGroup.objects.filter(id_nauczyciela=self.user.pk)
    # print(self.user.pk)
    self.fields['id_groups'].queryset = groups
👤Sayse

Leave a comment