jQuery远程验证没有显示错误消息DJango

时间:2014-12-02 06:27:54

标签: python django jquery-validate

我正在使用jQuery远程验证来检查是否可以通过调用DJango视图来注册电子邮件。

views.py

def check_email(request):
  is_available = "false"
  if request.is_ajax():
    username = request.POST.get("username")
    try: 
        User.objects.get_by_natural_key(username)
    except User.DoesNotExit:
       is_available = "true"
  return HttpResponse(is_available)

url.py

    url(r'^check_email/$', 'app.views.check_email', name='check_email')

的jQuery

{$( "#myform" ).validate({
rules: {
    email: { remote: "check-email/" }
}
messages: {
   email: {remote: "The email is already taken."}
}
});

jQuery代码未呈现错误消息。我错过了什么?感谢。

1 个答案:

答案 0 :(得分:2)

def check_email(request):
    is_available = "false"
    if request.is_ajax():
        username = request.GET.get("username") # Change post to get
        try: 
            User.objects.get_by_natural_key(username)
        except User.DoesNotExit:
            is_available = "true"
    return HttpResponse(is_available)

$( "#myform" ).validate({
    rules: {
        email: { 
            remote: "/check-email/",
            type: "get", // get method
        }
    }, // add comma
    messages: {
       email: {remote: "The email is already taken."}
    }
});