Why we have to return the value ? where does the value return ? | Sololearn: Learn to code for FREE!

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 "[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 AM

Levi

9 Answers

New Answer

+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

+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

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 ?

0

Please, show that piece of code

0

Yaroslav Vernigora https://code.sololearn.com/c1nzGrJRu4Qq/?ref=app

0

Yaroslav Vernigora Don't mind the class name!!

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

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

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 .