【问题标题】:django-parsley validations are not workingdjango-parsley 验证不起作用
【发布时间】:2014-03-19 19:01:28
【问题描述】:

我已经在 modelform 中完成了服务器端验证,并在我的项目中为注册表单自动生成了 HTML 文件,现在我想将其转换为客户端验证..我正在尝试通过提供参考链接来使用 parsley.js。 .它也呈现正确的文件,但 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>

【问题讨论】:

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


    【解决方案1】:

    在您的 html 文件中,您已将欧芹初始化放在样式标签中。将其放在脚本标签内。

      <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>
    

    【讨论】:

      猜你喜欢
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2013-12-02
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多