0

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 "fred@example.com" 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 ?

16th Aug 2022, 5:56 AM
Levi
Levi - avatar
9 Answers
+ 3
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
16th Aug 2022, 6:16 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
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
16th Aug 2022, 6:23 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
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 ?
16th Aug 2022, 7:31 AM
Levi
Levi - avatar
0
Please, show that piece of code
16th Aug 2022, 8:28 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Yaroslav Vernigora Don't mind the class name!!
16th Aug 2022, 10:48 AM
Levi
Levi - avatar
0
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
16th Aug 2022, 2:09 PM
Fu Foy
Fu Foy - avatar
0
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
16th Aug 2022, 2:27 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
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 .
16th Aug 2022, 4:27 PM
Levi
Levi - avatar