Hello, I want to know how Jango is_valid works.

Asked 2 years ago, Updated 2 years ago, 52 views

if self.is_valid():
    return redircet('/')

In the above code, is_valid() internally calls clean() of the form to validate it, but in the case of clean(), the return value is dict data called cleaned_date,

How can is_valid return True False? Is it supposed to return False if an element is added to the dict value of add.error or errors in clean()?

Please answer if you know. I don't understand. I'm so curious I ask for your respect. Thank you for reading it. I hope you have a good day for the rest of the day.

django python

2022-09-22 19:52

1 Answers

Well, don't be so flustered and let's take a look at Source step by step. First of all, is_valid().

def is_valid(self):
    return self.is_bound and not self.errors

In fact, this phrase itself is unconditionally returned with bool. Because the and operator is being used. I don't know what it is, but self.is_bound must be True and self.Errors must be Falsy to become a valid form.

Then self.What is errors?

@property
def errors(self):
    if self._errors is None:
        self.full_clean()
    return self._errors

self.errors is self by default.Property that returns _errors (self.If _errors is None, self.It turns full_clean() once, but does not return the result.)

At this point, your question - "self.clean() returns det, but is_valid() returns bool to some extent?" - is thought to be solved. is_valid() is a method in which True or False appears in the end, regardless of the mess.

Then, if an element is added to the value of add.error or dict of errors in clean(), does it return False? What is it?" - Let's check it out. According to what we've seen so far, is_valid() eventually self.When _errors does not have a value, it becomes False.

The question is self.The question is what _errors is.

def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                 initial=None, error_class=ErrorList, label_suffix=None,
                 empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None):
    # a heavy policy
    self.After _errors = None #clean() is called, the errors are stored here.

I translated an English comment. self.They save the errors in the _errors. How? Let's search.

def add_error(self, field, error):
    # a heavy policy
    for field, error_list in error.items():
        if field not in self.errors:
            # a heavy policy
            if field == NON_FIELD_ERRORS:
                self._errors[field] = self.error_class(error_class='nonfield') # <-- Something like this
            else:
                self._errors[field] = self.error_class() # <-- Things like this
        self._errors[field].extend(error_list)
        if field in self.cleaned_data:
            # <-- something like this

self as you know._errors is a list of errors. self.The syntax of _errors[field] is obvious.

Then when will self.add_error() be called?

def _clean_fields(self):
    for name, field in self.fields.items():
        # a heavy policy
        try:
            # a heavy policy
        except ValidationError as e:
            self.add_error(name,e) # <-- Here

And self.When does _clean_fields() invoke...

def full_clean(self): # --> Hmm? I think I've seen this method before
    # a heavy policy
    if self.empty_permitted and not self.has_changed():
        return

    self._clean_fields() # <-- Here
    self._clean_form()
    self._post_clean()

Oh! This is how it turns. From here, I think you can draw a validation processing flowchart of django form.

In short, the validation of django forms also seems to follow the methods of most validators. There is a variable/property that stores/updates a list of errors, and there is another variable/property that determines whether the list is empty or not.

Why should you do that? Can't you just make it simple like the code below? If you do...

if (invalid condition 1) {/ At first glance, there is no problem, but there is actually a problem.
    alert (Warning 1);
    return false;
}
If (invalid condition 2) { // ex) the invalid condition 2 syntax is error by itself, or if it is unconditionally true, or if it is a code injectable bug code, this section is simply unprepared.
    alert (Warning 2);
    return false;
}
If (Invalid Condition 3) { // ex) A user who violates all invalid conditions 1 to 3 must make at least three retries to receive all the warnings he or she should receive. The user is more than three times angry.
    alert (Warning 3);
    return false;
}
return true; // So codes that validate this way should actually be redesigned.

I don't know if the answer was helpful.


2022-09-22 19:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.