maneesh sandra
maneesh sandra

Reputation: 116

my django view is always throwing "unexpected keyword argument error

i am familiar with python and started to learn django and it is fun in the begining how ever a table insertion becomes nightmare to me from 2 days and always throwing the following error

sample() got an unexpected keyword argument 'firstname'

i have tried all the possible ways on documentation and stackoverflow but nothing works. but another view with same syntax got worked for me that's so weird. here are my files. this is my stack trace.

Traceback (most recent call last):
  File "C:\Users\manee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\manee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\manee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\manee\myapp\PE\views.py", line 77, in sample
    a=sample(firstname=name)

Exception Type: TypeError at /sample
Exception Value: sample() got an unexpected keyword argument 'firstname'

this is the combination of the worked view and troubling view(problems is working fine,sample is not working).

def problems(request):

    name=request.POST['name']
    title=request.POST['title']
    difficulty=request.POST['example']
    description=request.POST['description']
    solution=request.POST['solution']
    code=request.POST['code']

    question=problem(name=name,title=title,difficulty=difficulty,description=description,solution=solution,code=code)
    question.save()
    return render(request,'thanks.html')


def sample(request):
    name=request.POST['name']
    email=request.POST['email']
    rno=request.POST['rno']
    a=sample(firstname=name,email=email,rno=rno)
    a.save()
    return render(request,'example.html',{'name':name})

and here is my models.py

from django.db import models

#Create your models here.
class problem(models.Model):
    id=models.AutoField(primary_key=True)
    name=models.CharField(max_length=20)
    title=models.CharField(max_length=30)
    difficulty=models.CharField(max_length=10)
    description=models.TextField()
    solution=models.TextField()
    code=models.TextField()
    def __str__(self):
        return self.name

class sample(models.Model):
    firstname=models.CharField(max_length=20)
    email=models.EmailField()
    rno=models.IntegerField(primary_key=True)

    def __str__(self):
        return self.firstname

please get it fixed fast

Upvotes: 2

Views: 1275

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

Your sample model has no name field, only a firstname, but that is not the problem here. The problem is that your views have the same name, so if you call sample(..) it will call the view function.

Normally models start with an Uppercase, although not required, it makes it easier to make a distinction between the names of the models and the views. You thus can define the models as:

from django.db import models

#Create your models here.
class Problem(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    title = models.CharField(max_length=30)
    difficulty = models.CharField(max_length=10)
    description = models.TextField()
    solution = models.TextField()
    code = models.TextField()

    def __str__(self):
        return self.name

class Sample(models.Model):
    firstname = models.CharField(max_length=20)
    email = models.EmailField()
    rno = models.IntegerField(primary_key=True)

    def __str__(self):
        return self.firstname

In the views, you can then use the model with:

from .models import Problem, Sample

def problems(request):
    name=request.POST['name']
    title=request.POST['title']
    difficulty=request.POST['example']
    description=request.POST['description']
    solution=request.POST['solution']
    code=request.POST['code']
    question = Problem.objects.create(
        name=name,
        title=title,
        difficulty=difficulty,
        description=description,
        solution=solution,
        code=code
    )
    return render(request,'thanks.html')


def sample(request):
    name=request.POST['name']
    email=request.POST['email']
    rno=request.POST['rno']
    a = Sample.objects.create(firstname=name,email=email,rno=rno)
    return render(request,'example.html',{'name':name})

That being said, it is normally better to use a ModelForm [Django-doc] (or at least a Form to process input), this makes it less error-prone, and more convenient to process data.

Note: In case of a successful POST request, you should make a redirect [Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.

Upvotes: 3

Related Questions