Django表单is_valid()缺少1个必需的位置参数:'self'

时间:2019-11-03 08:17:38

标签: python django django-forms

我正在尝试创建一个form(boqform)以将数据发布到model(boqmodel)中 但我得到了

  

typeerror is_valid()缺少1个必需的位置参数:“ self”

我需要在html模板上显示一个名为boqform的表单,并将用户输入的数据发布到boqmodel中

我的观点:

def boqmodel1(request):

    if boqform.is_valid():
        form = boqform(request.POST)
        obj=form.save(commit=False)
        obj.save()
        context = {'form': form}
        return render(request, 'create.html', context)
    else:
        context = {'error': 'The post has been successfully created. Please enter boq'}
        return render(request, 'create.html', context)

跟踪:

File "C:\Users\TRICON\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\TRICON\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\TRICON\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\TRICON\Desktop\ind\ind\boq\views.py" in boqmodel1
  23.     if boqform.is_valid():

Exception Type: TypeError at /boq/create/
Exception Value: is_valid() missing 1 required positional argument: 'self'

1 个答案:

答案 0 :(得分:1)

您正试图在类is_valid()上调用实例函数boqform。您需要首先获取boqform的实例。切换功能的第1行和第2行应该可以解决此问题:

def boqmodel1(request):
    form = boqform(request.POST)
    if form.is_valid():
        obj=form.save(commit=False)
        obj.save()
        context = {'form': form}
        return render(request, 'create.html', context)
    else:
        context = {'error': 'The post has been successfully created. Please enter boq'}
        return render(request, 'create.html', context)