所有字段都给出该字段所需的错误

时间:2017-06-18 10:52:13

标签: django forms required

当我提交此表格时所有字段都正确填充,form.is _valid()返回false&所有字段都给出:这个字段是必需的错误,甚至是CharField !!!

任何人都可以看到有什么问题吗? 这是我的表格:

<form id="template_form"  method="post" role="form"  enctype="multipart/form-data" action="{% url 'create_templates' %}" >
 {% csrf_token %}

{{ form.as_p }}

    {% buttons %}
    <input type="submit"  value="Save Template"/>
  {% endbuttons %}



</form>

这是我的模板:

def create_templates(request):

    if request.method == 'POST':

        form = TemplateConfiguredForm(request.POST, request.FILES)

        if form.is_valid():

            template_configured = TemplateConfigured()
            template_configured.owner = request.user
            template_configured.logo = form.cleaned_data["logo"]
            template_configured.image = form.cleaned_data["image"]
            template_configured.message = form.cleaned_data["message"]

            template = form.cleaned_data['template']

            template = dict(form.fields['template'].choices)[template]

            template_configured.template = Template.objects.get(name = template)

            template_configured.save()
            saved = True


        else:
            print form.errors


    else:
        form = TemplateConfiguredForm()


    return render(request, 'sendMails/createTemplates.html', locals())

这是我的观点:

var elems=Array.prototype.filter.call(querySelectorAll('div.bar'),function(el){
  var foochild=false;
  while(el=el.parent){
    if(el.classList.contains("bar") return false;//fail as its a div.bar children
    if(el.classList.contains("foo")) foochild=true;//were a children of foo
   }
   return foochild;
});

2 个答案:

答案 0 :(得分:2)

您已更改表单的签名,以便第一个位置参数为custom_choices。不要那样做。

您似乎根本没有从视图中传递该值,因此您可能应该完全删除它。但如果你确实需要它,你应该从kwargs dict中获取它:

def __init__(self, *args, **kwargs):
    custom_choices = kwargs.pop('custom_choices')
    super(TemplateConfiguredForm, self).__init__(*args, **kwargs)

答案 1 :(得分:1)

您在表单中传递的数据,此处:

form = TemplateConfiguredForm(request.POST, request.FILES)

由您签名的第一个关键字参数捕获:

def __init__(self, custom_choices=None, *args, **kwargs):

删除custom_choices=None