首次调用表单时禁用验证 [英] Disable validation when calling form for the first time

查看:90
本文介绍了首次调用表单时禁用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Django表单时有些挣扎.当我呼叫表单站点时,总是会出现验证错误(此字段为必填项).如果不像javascript函数那样填写字段,我希望在单击提交"按钮后看到此消息.另外,我使用正则表达式进行验证,效果很好.

I am struggling a bit with my Django forms. When I call my form site, always validation errors appear (this field is required). I'd prefer to see this message after clicking the submit button, if a field is not filled like a javascript function would do. In addition I'm using regex for validation, which is working fine.

我正在使用CVB.这是一些代码:

I am working with CVBs. Here is some code:

models.py

models.py

 class Institute(models.Model):
    name = models.CharField(max_length=50)
    timestamp = models.DateTimeField(auto_now_add=True)  

views.py

class InstituteCreate(CreateView):
    model = Institute
    form_class = InstituteForm
    success_url = reverse_lazy('institute_list')

forms.py

class InstituteForm(forms.ModelForm):

    name= forms.CharField(error_messages={'required': 'Own Error Text'}, validators=[RegexValidator(regex='^[a-zA-ZäüößÄÜÖ]*$', message='forbidden string', code='string_invalid')])

    class Meta:
        model = Institute
        fields = ['name']

希望有人对如何解决它有所了解.

Hope someone has an idea on how to fix it.

edit1:

我的模板很简单

{% block pagetitle %}Institutes{%endblock %}
{% block content %}
    <form class="form-horizontal" name="form_group" method="post">
        {% csrf_token %}
            <div>
                {{ form.as_p }}
            </div>
            <input class="btn btn-primary" type="submit" value="click me" />
    </form>
{% endblock %}

和我的网址配置:

urlpatterns = patterns('',  
    url(r'^institute_create/$', views.InstituteCreate.as_view(), name='institute_create'),
)

我是Django开发的新手,所以我将尝试更详细地解释问题:

I'm new to Django development so i'll try to explain the problem more detailed:

在我的网站上,当我打开链接www.exampleurl.com/institute_create时,将显示我的表单.然后,我看到必须在其中输入研究所名称的字段.在该字段上方显示文本此字段是必需的".但是我不想看到这个,直到我尝试提交一个空表格.

On my website, when i open the link www.exampleurl.com/institute_create my form is shown. Then i see the field where i have to enter the name for the institute. Above this field the text "this field is required" is displayed. But i don't want to see this, until i try to submit an empty form.

当我输入不匹配的文本并按提交按钮时,错误文本字段会按预期将其消息更改为禁止的字符串.

When i enter some text which doesnt match and i press submit button the error text field changes its message to forbidden string as expected.

推荐答案

除非对视图使用POST请求,否则不会触发表单验证.您的代码中的其他地方可能存在错误,但是,您需要解决的一些有关代码的问题:

Unless you're using a POST request to your view, form validation won't be triggered. There's likely an error somewhere else in your code, however, there are couple of things about your code that you'll want to address:

Python中的类应始终以大写字母开头,并遵循

Classes in Python should always begin with an upper-case letter and follow the CapWords convention:

class Institute(models.Model):
    name = models.CharField(max_length=50)

    # just use the built-in `auto_now_add` argument
    timestamp = models.DateTimeField(auto_now_add=True)

class InstituteCreate(CreateView):
    model = Institute
    form_class = InstituteForm
    success_url = reverse_lazy('institute_list')

class InstituteForm(forms.ModelForm):

    # All Django model/form fields are required by default, so you
    # can drop the `required=True` here
    name= forms.CharField(validators=[RegexValidator(regex='^[a-zA-ZäüößÄÜÖ]*$',
        message='forbidden string', code='string_invalid')])

class Meta:
    model = Institute
    fields = ['name']

否则,无法分辨出类定义和类实例之间的区别,并且碰到碰撞的可能性也大大降低.

Otherwise, it's impossible to tell the difference between the class definition and an instance of the class, and you're a lot less likely to run into collisions.

出于好奇,您是否看到浏览器内HTML5验证错误与Django错误?如果您可以将模板代码添加到问题中,则可能会有所帮助.

Just out of curiosity, are you seeing in-browser HTML5 validation errors versus errors from Django? If you can add your template code to your question it might help.

这篇关于首次调用表单时禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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