I am creating a form which will allow a user to send an email to multiple people (students).
I have used ModelMultipleChoiceField to create checkboxes for each user, however I’m not sure how to deal with the data that gets posted.
Here’s my view so far:
if request.method == 'POST':
subject = request.POST['subject']
message = request.POST['message']
email = EmailMessage(subject, message, 'from@example.com',
recipient_addresses)
email.send()
else:
students = Student.objects.exclude(email='')
form = StudentListForm(students=students)
The form just posts the ID numbers of the selected recipients. Do I have to filter Student objects like this:
Student.objects.filter(pk__in=request.POST['students'])
Or is there a ‘better’ way?
Any advice would be appreciated.
Thanks
You’re missing most of the point of using a form, which is to rely on it for validation and data conversion, as well as simply showing fields in HTML.
Basically, you should always access
form.cleaned_datainstead ofrequest.POST.