django-parsley验证不起作用

时间:2014-03-19 19:01:29

标签: javascript django validation client-side parsley.js

我已经在模型中进行了严格的侧面验证,并在我的项目中自动生成了用于注册表单的HTML文件,现在我想将其转换为客户端验证。我试图通过仅提供参考来使用parsley.js link..its渲染正确的文件,但parsley.js的验证不起作用..

请帮帮我... 这是我的模式类代码:

<code>
 `class Organization(models.Model):
  name = models.CharField(max_length=100, 
                        blank = False,  
                        null = False, 
                        help_text = "Please enter full name without abbreviations",
                        verbose_name = "Organization's Full Name:",
                        unique = True,
                        error_messages = 
                  {'unique': "Your organization has already signed up with us!" },
                        validators = [organization_name_validator],
                        )
    website = models.URLField(verbose_name = 'Website of your organization:',

    help_text = "Please enter the complete URL of your      organization's website",
                          null = True,
                          blank = True,
                         validators = [url_validator],
                          )
     #TODO: the following should have some custom phone number type of field
    phone_number = models.CharField(max_length=18,
                                blank = False,
                                null = False,

                                help_text = ""
                                verbose_name = "Official Phone Number",
                                unique = True,
                                error_messages = 
  {'unique' : 'Looks like an has already signed up!'},
                                validators = [phone_number_validator]
                                )

   registered_on = models.DateTimeField(auto_now_add = True)


  active = models.BooleanField(default = True,
                             verbose_name = 'Organization enabled ?',
                             help_text = ''
                              )

   signed_up_by_role = models.CharField(max_le ngth = 12,
                                    blank = False,
                                    null = False,
                                    verbose_name = ':',
                                    help_text = ""
                                   )


  def clean(self):

    if len(self.phone_number.strip()) == 0:
        raise ValidationError("Phone number cannot be empty.")


    if self.name.strip() == '':
        raise ValidationError("Name cannot be empty.")

   def __unicode__(self):
    return self.name`

这是我渲染的html文件,我已经为了欧芹集成目的进行了修改:

</code>   
<b>

<!DOCTYPE html>
{% load static %}

<html>
<head>
    <title> {% block title %}Welcome to laresumex!{% endblock %}</title>
<script language='javascript' src="{% static 'js/jquery-2.1.0.js' %}"> </script>
    <script language='javascript' src="{% static 'js/parsley.min.js' %}"> </script>
<style type='text/css'>
        ul.parsley-error-list {
            font-size: 11px;
            margin: 2px;
            list-style-type:none;
        }
        ul.parsley-error-list li {
            line-height: 11px;
            color: #b94a48;
            background: #f2dede;
            border: 1px solid #eed3d7;
        }   
        $(document).ready(function () {
      $('#demo-form').parsley()
  });

</style>



</head>
<body>

 {% block body_block %}
<div class="hero-unit">
<h1>Organization signup form</h1>        
<div class="container">
<form id='form' data-parsley-validate method="post" >
{# cross-site-request-forgery-prevention #}
{% csrf_token %}

{% for field in form.visible_fields %}
        {{ field.label_tag }} {{ field }} 
         {# we have to use field.field here because of visible_fields overlay #}
        {% if field.field.required %}
            <sup>*</sup>
        {% endif %}
        <sub>{{ field.help_text }}</sub>
        <br/>{{ field.errors }}

{% endfor %} 

    {# Include the hidden fields #}
{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}
{# Include the visible fields #}

<div>
    <p><input type="submit" class="btn" value="Signup!" /></p>
</div>

</form>

</div>
{% endblock %}
</body>
</html>'
</b>
</code>

1 个答案:

答案 0 :(得分:0)

在你的html文件中,你已将parsley初始化放在样式标记内。把它放在脚本标签内。

  <style type='text/css'>
    ul.parsley-error-list {
        font-size: 11px;
        margin: 2px;
        list-style-type:none;
    }
    ul.parsley-error-list li {
        line-height: 11px;
        color: #b94a48;
        background: #f2dede;
        border: 1px solid #eed3d7;
    }
 </style>
 <script>
    $(document).ready(function () {
      $('#demo-form').parsley()
    });
 </script>