1

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'
thebjorn
  • 26,297
  • 11
  • 96
  • 138
Raviteja Reddy
  • 193
  • 1
  • 5
  • 16
  • `missing 1 required positional argument: 'self'` is a [very common Python error](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) that usually indicates that you are calling an instance method against a class rather than an object. Can you post the stack trace? From which line is the error originating? – mario_sunny Nov 03 '19 at 08:21
  • 1
    What is `boqform` defined as? Edit: I think I see the issue... – mario_sunny Nov 03 '19 at 08:25

1 Answers1

1

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)
mario_sunny
  • 1,412
  • 11
  • 29