views:

1467

answers:

3

Hi,

I have a "Reset Password" form being validated by the jQuery Validation plugin. Everything works fine except that I can't get the "remote" part to work with Django. What I'm trying to do is to have remote send an ajax request to my Django backend to check that the "Old Password" is correct as part of validation.

What I'm not sure about is how to set up my url.py and views.py to return what jQuery.validation needs

Login is required, so there will be a request.user

My Code:

    $("#changePassword").validate({
    rules:
    {
        oldPassword:
        {
            required: true,
            remote:
            {
                url: "profile/password/check/",
                type: "post",
                data:
                {
                    oldPassword: function()
                    {
                        return $("#oldPassword").val();
                    }
                }
            }
        },
        newPassword: "required",
        confirmPassword:
        {
            equalTo: "#newPassword"
        }
    }
});

Help is much appreciated, thank you :)

A: 

Hmm i did something like that in my django project. since im not behind the computer where i program and dont have access to my code i can only describe what i did.

I urls py i have set up some url like

/profile/save/, which calls saveProfile view. You could set up url like ^pwdvalidate/$ and call matching view.

In saveProfile view i do check

if the request.is_ajax() and if request is get. then check request.user's existing password and do all other checks you need. Return it with simplejson dumps: return HttpResponse(simplejson.dumps(some_python_obj))

js request do with jquery.ajax

I understand its quite vague and generic, but hope it helps.

Edit: Check out this example : http://webcloud.se/article/AJAX%5Fin%5FDjango%5Fwith%5FjQuery/

Zayatzz
+1  A: 

All you need to do is map the URL ^profile/password/check/ to a view, and within that view, fetch the value for the key oldPassword in the request.POST dictionary, do with it what you will, and return either true or false. For example:

def check_profile_password(request):
  response_str = "false"
  if request.is_ajax():
    old_password = request.POST.get("oldPassword")
    if old_password:
       # Apply whatever logic you want to apply
       response_str = "true"

  return HttpResponse("%s" % response_str)
ayaz
This answer is somewhat helpful, but you haven't mentioned anything about the jQuery validation API
Alex
Alex, I wrote that code taking into account the jQuery code that you have presented in your question. I see nothing wrong in your jQuery code. My answer explains how you would set up your urls.py and views.py to work with the remote validation rule specified in the jQuery snippet.
ayaz
+1  A: 

Okay I ended up figuring it out myself, thanks though to everyone who offered help.

From the jQuery.Validation side, I think perhaps my methods weren't getting through or being added to the Validation modeul properly, so there was never anything wrong with Django.

I ended up simply putting "remote:" into my input tag, like so:

<input id="oldPassword" name="oldPassword" type="password" remote="/profile/password/check/" size="25" class="required" minlength="2" />

Then to apply validation, in a tag:

$("#changePassword").validate();

Then in Django:

old_password = request.GET.get("oldPassword")
request_user = User.objects.get(id=request.user.id)
if(request_user.check_password(old_password) == True):
    response_str = "true"
return HttpResponse(response_str)
Alex