Home > front end >  its showing me that "book() got an unexpected keyword argument 'name'"
its showing me that "book() got an unexpected keyword argument 'name'"

Time:12-29

its showing me that "book() got an unexpected keyword argument 'name' "

my views.py

def book(request):
    if request.method=='POST':
        name=request.POST['name']
        email=request.POST['email']
        password=request.POST['password']
        type=request.POST['r']
        ticket_from=request.POST['ticket_from']
        ticket_to=request.POST["ticket_to"]
        dept_date=request.POST['dept_date']
        ret_date=request.POST['ret_date']
        adults=request.POST['adults']
        children=request.POST['childern']
        tck=book(name=name,email=email,password=password,type=type,ticket_from=ticket_from,ticket_to=ticket_to,dept_date=dept_date,ret_date=ret_date,adults=adults,children=children)
        tck.save()
        return HttpResponse("Booked")   


    else:
        return render(request,"book.html",)

models.py
# Create your models here.
class book(models.Model):
    name=models.CharField(max_length=100)
    email=models.EmailField(max_length=100)
    password=models.CharField(max_length=100)
    type=models.CharField(max_length=100)
    ticket_to=models.CharField(max_length=100)
    ticket_from=models.CharField(max_length=100)
    dept_date=models.DateField()
    ret_date=models.DateField()
    adults=models.IntegerField()
    children=models.IntegerField()
    

i dont know what is this error and how can i solve this plzz help me with this

CodePudding user response:

Always use Capital letters for class names. Your code actually is trying to use view's function instead of model's class. You have used exactly the same name, which is very confusing.

Change this:

class book(models.Model):
    ...

To this:

class Book(models.Model):
    ...

And always create new object with first Capital letter:

def book(request):
    if request.method=='POST':
        ...
        tck = Book(name=name, email=email, ...)
        tck.save()
        return HttpResponse("Booked")

And for more complex class' names always start new word with another capital letter, in example:

class VeryOldBook(models.Model):
    ...

Read that for all the important rules: https://realpython.com/python-pep8/

CodePudding user response:

views.py

def book(request):
    if request.method=='POST':
        name=request.POST['name']
        email=request.POST['email']
        password=request.POST['password']
        type=request.POST['r']
        ticket_from=request.POST['ticket_from']
        ticket_to=request.POST["ticket_to"]
        dept_date=request.POST['dept_date']
        ret_date=request.POST['ret_date']
        adults=request.POST['adults']
        children=request.POST['childern']
        tck=Book(name=name,email=email,password=password,type=type,ticket_from=ticket_from,ticket_to=ticket_to,dept_date=dept_date,ret_date=ret_date,adults=adults,children=children)
        tck.save()
        return HttpResponse("Booked")   


    else:
        return render(request,"book.html",)

models.py

class Book(models.Model):
    name=models.CharField(max_length=100)
    email=models.EmailField(max_length=100)
    password=models.CharField(max_length=100)
    type=models.CharField(max_length=100)
    ticket_to=models.CharField(max_length=100)
    ticket_from=models.CharField(max_length=100)
    dept_date=models.DateField()
    ret_date=models.DateField()
    adults=models.IntegerField()
    children=models.IntegerField()

this should solve your problems

  • Related