gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:00:06 26 4
gpt4 key购买 nike

我正在尝试创建一个表单(boqform)以将数据发布到模型(boqmodel)但我得到了这个

typeerror is_valid() missing 1 required positional argument: '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'

最佳答案

您正在尝试调用类 boqform 上的实例函数 is_valid()。您需要首先获取 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)

关于python - Django 表单 is_valid() 缺少 1 个必需的位置参数 : 'self' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com