A brief introduction we talk about how to build forms to hook
two aim to implement a custom authentication mechanism, so as to process
three categories:
1 partial hook clean_column () when the field validation is complete, and then to find out if there is a function that begins clean_ name, if there is, it calls the function
2 global hook clean () the principle is completed checksum field, and then check between the fields. When the field validation is completed, whether a lookup function has a clean, if you run the function
execution order partial global hook hook
three basic syntax
from the ValidationError django.core.exceptions Import, NON_FIELD_ERRORS
DELF clean_column (Self):
value = self.cleaned_data. GET ( 'Key')
IF to true:
return value
the else:
The raise the ValidationError ()
Four override clean () method
clean () method is not predefined process flow returns only cleaned_data
five Note
1 a single field specify only a single method
2 in accordance with the hook, but if the above, the exception is thrown directly is not executed in the order performs the following hook
3 clean () highest priority
six exemplary scenario
1 is inserted into the presence of the user is not allowed to (partially hooks)
DEF clean_user (Self):
user1 = self.cleaned_data.get ( "user")
Val = the user. objects.filter (= user user1)
IF Not Val:
return Val
the else:
the raise the ValidationError ( "user name already exists")
error {{field.errors}} according to the display
confirmation 2 and a secondary password entered password (global hook )
DEF Clean (Self):
pwd = self.cleaned_data.get ( "password")
pwd1 = self.cleaned_data.get ( "password_1")
IF pwd == pwd1:
self.cleaned_data return
the else:
The raise the ValidationError ( "password twice inconsistent")
views function
g_error = form.errors.get ( "__ all __ ") # global hook acquired in this way given the information
distal
{% if filed.label == " confirm password "%}
{} {} g_error
{%} endif%