Raviteja Reddy
Raviteja Reddy

Reputation: 193

Django Form is_valid() missing 1 required positional argument: 'self'

I am trying to create a form(boqform) to post data into a model(boqmodel) but I am getting this

typeerror is_valid() missing 1 required positional argument: 'self'

I need to display a form called boqform on html template and post the data user entered into boqmodel

my view:

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)

Traceback:

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'

Upvotes: 1

Views: 1242

Answers (1)

mario_sunny
mario_sunny

Reputation: 1432

You're trying to call the instance function is_valid() on the class boqform. You need to get an instance of boqform first. Switching lines 1 and 2 of the function should fix the issue:

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)

Upvotes: 1

Related Questions