TypeError:is_valid()缺少1个必需的位置参数

时间:2019-08-18 06:31:11

标签: python django forms view django-2.0

***强文本***我已经成功编写了模型和表单文件,但是在传递参数后仍然在视图中显示一些错误,因为在定义self后未定义self,这表明NoneType对象没有属性'is_bound < / p>

from django.shortcuts import render
from .models import personal_data,professional_career
from django.http import HttpResponse
from .form import detail , details2

def cricket_form(request, self=None):
    if request.method == 'POST':
        form=detail(request.POST)
        form=detail2(request.POST)

    if detail.is_valid(self) or detail2.is_valid(self):
        form.save()

    context ={

    }
    return render(request,"template/createform.html",context)

1 个答案:

答案 0 :(得分:1)

错误NoneType对象没有属性is_bound是 因为您要在表单类is_valid上调用detail方法,而不是detail instance对象form对象。

将代码更改为:

form1 = detail(request.POST) # Better to user uppercase class name(Detail)
form2 = detail2(request.POST)
if form1.is_valid() or form2.is_valid():
    ...