cleaned_data in django forms
what is cleaned_data in django
Any 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.
Eventually, the browser would will everything as strings. When Django cleans the data it automatically converts data to the appropriate type.
For example IntegerField data would be converted to an integer
In Django, this cleaned and validated data is commonly known as cleaned data.
We can access cleaned data via cleaned_data dictionary:
# name = form.cleaned_data['name']
5
1
form.cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects.
form.data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).
Thank you!
1
0
3.63
8
from django.core.mail import send_mail
if form.is_valid():
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
sender = form.cleaned_data['sender']
cc_myself = form.cleaned_data['cc_myself']
recipients = ['[email protected]']
if cc_myself:
recipients.append(sender)
send_mail(subject, message, sender, recipients)
return HttpResponseRedirect('/thanks/')
Thank you!
8
0
Are there any code examples left?
New code examples in category Python