python - django form.is_valid() 总是返回 false

标签 python django forms dajaxice dajax

在我看来,My is valid 函数似乎总是返回 false,即使该帖子似乎发送了正确的数据(我认为)。我对 django 和 python 还很陌生,我使用 Django 文档作为指南。我还尝试使用 dajax 中的 django 表单示例来实现我的表单(我已经在同一个项目中成功安装并使用了 dajax)。我的其他 Dajax 方法是 get 方法,我确信它们不会干扰我的帖子。

每次我点击帖子时,都会出现“表单无效”错误警报,我不确定为什么它不进入 if block 。

我在这里读过类似的问题,并仔细检查了我能想到的所有内容。如果有人能帮忙指出问题出在哪里,我将不胜感激。我正在下面粘贴所需的代码。

forms.py

class BedSelectForm(forms.Form):
Branch = forms.ModelChoiceField(
    label = u'Branch',
    queryset = Result.objects.values_list('branch', flat =True).distinct(),
    empty_label = 'Not Specified',
    widget = forms.Select(attrs = {'onchange' : "Dajaxice.modmap.updatecomboE(Dajax.process, {'optionB':this.value})"})
    )
Env = forms.ModelChoiceField(
    label = u'Environment',
    queryset = Result.objects.values_list('environment', flat =True).distinct(),
    empty_label = 'Not Specified',
    widget = forms.Select(attrs = {'onchange' : "Dajaxice.modmap.updatecomboD(Dajax.process, {'optionE':this.value})"})
    )
Disc = forms.ModelChoiceField(
    label = u'Discipline',
    queryset = Result.objects.values_list('discipline', flat =True).distinct(),
    empty_label = 'Not Specified'

    )

template.html

<form action="" method="post" id = "select_form">
        <div style = "color :white" class="field_wrapper">
             {{ form.as_p }}
        </div>        
        <input type="button" value="Display Table" onclick="send_form();"/>
</form>

<script type="text/javascript">
    function send_form(){
        Dajaxice.modmap.dispTable(Dajax.process,{'form':$('#select_form').serialize(true)});
    }
 </script>

ajax.py

@dajaxice_register
def dispTable(request, form):
    dajax = Dajax()
    form = BedSelectForm(deserialize_form(form))

    if form.is_valid():
        dajax.remove_css_class('#select_form input', 'error')
        dajax.alert("Form is_valid(), your username is: %s" % form.cleaned_data.get('Branch'))
    else:
        dajax.remove_css_class('#select_form input', 'error')
        for error in form.errors:
            dajax.add_css_class('#id_%s' % error, 'error')
        dajax.alert("Form is_notvalid()")

    return dajax.json()

这就是我的帖子的样子..

argv    {"form":"Branch=Master&Env=ProdCoursera&Disc=ChemistryMOOC"}

最佳答案

尝试将 {{ form.non_field_errors }} 添加到您的模板中。这将向您显示与字段无关的错误。

此外,如果可以的话,为了调试,请执行此操作而不是 {{ form.as_p }}

{% for field in form.fields %}
    <div>{{ field }}</div>
    {% if field.errors %}
            <div class="alert alert-danger">
                {{ field.errors }}
            </div>
    {% endif %}
{% endfor %}

看看POSTing 时是否显示一些错误。

关于python - django form.is_valid() 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24767788/

相关文章:

python - Django Python ...更新数据库中的多个元素

python - Django 模板图像无法加载(重新发布)

python - Django 1.7 : how to make ManyToManyField required?

javascript - 选择文件后立即提交表格

python - pyTelegramBotAPI。如何在 next_step_handler 解决方案中保存状态?

python - 这是在 python 中标记 csv 文件以转换为 json 文件的最佳方法

python - Plotnine 旋转标签

python - 来自 "Generic related"模型的 Django 表单

javascript - JQuery - 如何发送表单并调用 anchor ?

asp.net-mvc - HTML 表单上 ASP.NET MVC 中的 PUT 或 DELETE 动词