Why we have to return the value ? where does the value return ?
from django import forms from django.core.exceptions import ValidationError class ContactForm(forms.Form): # Everything as before. ... def clean_recipients(self): data = self.cleaned_data['recipients'] if "[email protected]" not in data: raise ValidationError("You have forgotten about Fred!") return data There is return data at the end. whats the need to return the data ? and where does it return ?
8/16/2022 5:56:14 AMLevi
9 AnswersNew Answer
You need to return data when you use functions in your program. the main program, when calling and executing a function, does not see any data and calculation results within the function itself. therefore, there is a need to return calculations to the main program. data is returned to the main program
in your case, in the code of the main program, you create a function with the name "clean_recipients" and put the value of the variable "self" inside. inside this function, you create a "data" variable and compute it. "data" is not visible to the main program. so that the main program can use the "data" value, you return it to the main program
Yaroslav Vernigora But in my case, a dictionary named cleaned_data already has the data of the form filled by the user. So what is the use of return ?
Please, show that piece of code
Yaroslav Vernigora https://code.sololearn.com/c1nzGrJRu4Qq/?ref=app
Yaroslav Vernigora Don't mind the class name!!
When your form is valid you get cleaned_data. Before that your clean_fieldname is called. In your case clean_recipients. And the value that this method returns will be in the forms cleaned data. Hope that helps
it is difficult for me to analyze your second piece of code, what it does and how it is related to the code at the very beginning of your question, but I can see that you import the library or the ready-made render (import render keywords) function into the program. then create your register function and put the request value inside it. inside the function, calculations and various kinds of transformations take place and ultimately the last line is the return render command... i.e. the register function returns to the main program the value or result of the render execution
Fu Foy But even if I don't give any return statement, if I print the clean_data dictionary I get the data that is entered by the user .