jQuery Validation Plugin使用Django远程检查密码 [英] jQuery Validation Plugin remote check for password with Django

查看:74
本文介绍了jQuery Validation Plugin使用Django远程检查密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由jQuery Validation插件验证的重置密码"表格.一切工作正常,除了我无法使用"remote"部分来使用Django.我想做的是让远程向我的Django后端发送一个ajax请求,以检查旧密码"是否正确,这是验证的一部分.

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.

我不确定如何设置url.py和views.py以返回jQuery.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

我的代码:

    $("#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 :)

推荐答案

好吧,由于每个人都提供了帮助,我最终自己弄清楚了.

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

从jQuery.Validation的角度来看,我认为我的方法可能没有通过或未正确添加到Validation模版中,因此Django从未出错.

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.

我最终只是将"remote:"放到了输入标签中,就像这样:

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();

然后在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)

这篇关于jQuery Validation Plugin使用Django远程检查密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆