Home > database >  TypeError at / Accept() got an unexpected keyword argument 'name'
TypeError at / Accept() got an unexpected keyword argument 'name'

Time:07-22

I want to take data from user and save it to the database by using Django. Have tried to solve it. But I am not able to solve this problem and didn't find any working solution on the internet.

I'm getting this errorenter image description here

My views.py file

def Accept(request):
    if request.method == "POST":
        name = request.POST.get("name","")
        phone = request.POST.get("phone","")
        email = request.POST.get("email","")
        school = request.POST.get("school","")
        degree = request.POST.get("degree","")
        university = request.POST.get("university","")
        skill = request.POST.get("skill","")
        about_you = request.POST.get("about_you","")
        previous_work = request.POST.get("previous_work","")

        accept = Accept(name=name,phone=phone,email=email,school=school,degree=degree,university=university,skill=skill,about_you=about_you,previous_work=previous_work)
        accept.save()
    return render(request,"accept.html")

models.py

class profile(models.Model):
    name = models.CharField(max_length=255)
    phone = models.CharField(max_length=12)
    email = models.EmailField(max_length=100)
    school = models.CharField(max_length=100)
    degree = models.CharField(max_length=100)
    university = models.CharField(max_length=100)
    skill = models.TextField(max_length=1000)
    about_you = models.TextField(max_length=1000)
    previous_work = models.TextField(max_length=1000)

accept.html

{% csrf_token %} Name Phone Email School Degree University Skills About You Previous Work Submit enter code here

Here is the full traceback error enter image description here

CodePudding user response:

I think what might be the issue is that you are calling Accept to register a profile inside the Accept function, so basically you are calling the function and not the object profile

Suggested changes:

def Accept(request):
    if request.method == "POST":
        name = request.POST.get("name","")
        phone = request.POST.get("phone","")
        email = request.POST.get("email","")
        school = request.POST.get("school","")
        degree = request.POST.get("degree","")
        university = request.POST.get("university","")
        skill = request.POST.get("skill","")
        about_you = request.POST.get("about_you","")
        previous_work = request.POST.get("previous_work","")

        # dont call the function but the object profile
        accept = profile(name=name,phone=phone,email=email,school=school,degree=degree,university=university,skill=skill,about_you=about_you,previous_work=previous_work)
        accept.save()
    return render(request,"accept.html")

Also in python, there is some naming convention: Function name should be in lowercase while class usually use CapsWord convention : https://peps.python.org/pep-0008/#naming-conventions

CodePudding user response:

The Accept you're creating here refers to your view name, so you're trying to create a view with all these kwargs, which obviously won't work.

I think you wanted to create a profile instance and wrote Accept(..) instead of profile(..).

  • Related