2016-04-02 4 views
0

私はParsley.jsとDjangoでフロントエンドユーザー認証チェックを設定しています。 これが私の見解json.dumpsの値の1つがnullです

@requires_csrf_token 
def password_check(request): 
    email = request.POST.get('email') 
    password = request.POST.get('password1') 
    user = authenticate(email=email, password=password) 

    if request.is_ajax(): 
     if user and user.is_active: 
      res = "These password and e-mail are ok." 
      ajax_vars_login = {'response': res, 'email': email, 'password': password} 
      json_data_login = json.dumps(ajax_vars_login) 
     else: 
      res = "The e-mail or the password field aren't correct." 
      ajax_vars_login = {'response': res, 'email': email, 'password': password} 
      json_data_login = json.dumps(ajax_vars_login) 

     return HttpResponse(json_data_login, content_type='application/json') 

であり、これはパセリのバリである:私は上を押したときに提出するため

Parsley.addAsyncValidator(
    'emailPwCombination', function (xhr) { 
     var password = $('#password').parsley(); 
     var email = $('#email').parsley(); 
     var response = xhr.responseText; 
     var jsonResponse = JSON.parse(response); 
     var jsonResponseText = jsonResponse["response"]; 

     if(jsonResponseText == 'These password and e-mail are ok.') 
      return true; 
     if(jsonResponseText == '404') 
      return false; 
    }, '/password_check/' 
); 

問題は、私は、電子メールがサーバーに送信されていないように見えるです2つのxhr応答を得る。
最初の応答:

{password: null, response: "The e-mail or the password field aren't correct.",…} 
email: "[email protected]" 
password: null 
response: "The e-mail or the password field aren't correct." 

秒の応答:

{password: "examplepassword", response: "The e-mail or the password field aren't correct.", email: null} 
email: null 
password: "examplepassword" 
response: "The e-mail or the password field aren't correct." 

私は何をしないのですか?

+0

ここではhtmlです –

答えて

2

このコードは非同期バリデータの内部にあるので、値の上で.parsley()を呼び出すたびに、そのコードはバックエンドに送信されます。したがって、2つの別々のリクエストがあります.1つは電子メールだけで、もう1つはパスワードだけです。

私はあなたがパセリが何であるかを理解していないと思います。これは一連の基準に基づいて個々のフィールドを検証するためのものです。処理のためにバックエンドに完全な形式のデータを送信するためのものではありません。これは基本的なjQueryのいくつかの行で非常に簡単に行うことができます。ここではパセリは必要ありません。

+0

はい、私は個々のフィールドconvalidationとフォーム処理のいずれかと思っていました。あなたの答えをありがとう:) –

関連する問題