1what is cleaned_data in django
2
3Any data the user submits through a form will be passed to the server as strings. It doesn't matter which type of form field was used to create the form.
4Eventually, the browser would will everything as strings. When Django cleans the data it automatically converts data to the appropriate type.
5For example IntegerField data would be converted to an integer
6In Django, this cleaned and validated data is commonly known as cleaned data.
7We can access cleaned data via cleaned_data dictionary:
8 # name = form.cleaned_data['name']
1from django.core.mail import send_mail
2
3if form.is_valid():
4 subject = form.cleaned_data['subject']
5 message = form.cleaned_data['message']
6 sender = form.cleaned_data['sender']
7 cc_myself = form.cleaned_data['cc_myself']
8
9 recipients = ['info@example.com']
10 if cc_myself:
11 recipients.append(sender)
12
13 send_mail(subject, message, sender, recipients)
14 return HttpResponseRedirect('/thanks/')
15
1form.cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects.
2
3form.data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).