2. In the user name location, the mouse leaves the lost focus, uses AJAX to detect whether the user name exists, and displays2.1find the JS file of the checksum front-end input function check_user_name () {var len= $('#user_name'). val (). length; if(len<5| | Len>20) { $('#user_name'). Next (). HTML ('Please enter a 5-20-character user name') $('#user_name'). Next (). Show (); Error_name=true; } Else { //after the verification of the front-end format is successful, add a mouse to lose focus, to determine whether the user name exists;//through the AJAX request view, Jsonresponse returns the judgment result;Ajax Get Methods 3 parameters: 1. Access view; 2. Parameters to be carried by the request; 3. Success execution function after successful;//parameters in the execution function: data is the dictionary returned by the view Jsonresponse; $.get ('/users/check_useranme/', {'username': $('#user_name'). Val ()}, function (data) {//Displays the judging information according to the Returned judgment information .if(Data.ret = = 1){ $('#user_name'). Next (). HTML ('User name exists'). Show (); Error_name=true; } Else{ $('#user_name'). Next (). Hide (); Error_name=false; } }); } 2.2view functions for validationdefCheck_username (Request):#If the user existsUsername = post.get (Request,'username') ifModels. User.objects.get (user_name=username):returnJsonresponse ({'ret': 1}) Else: returnJsonresponse ({'ret': 0})
Whether the backend user name exists validation | Show based Ajax when Mouse loses focus | Django Development