Suraj
Suraj

Reputation: 155

TypeError in django while creating model instances

I have a model onlinebooking and I am trying to save the data the user inputs. However I am getting the error TypeError at /onlinebooking/ onlinebooking() got an unexpected keyword argument 'name'. I get this error after clicking the register button.

Here is my model:

class onlinebooking(models.Model):
    name = models.CharField(max_length=30)
    email = models.CharField(max_length=30)
    phone_number = models.IntegerField()
    room_type = models.CharField(max_length=10)
    booking_date = models.DateField()

views.py

from django.shortcuts import render,redirect
from .models import onlinebooking

def onlinebooking(request):
    if request.method == "POST":
        name = request.POST['Name']
        email = request.POST['email']
        phone_number = request.POST['phone_no']
        room_type = request.POST['room_type']
        booking_date = request.POST['booking_date']
        online = onlinebooking(name=name,email=email,phone_number=phone_number,room_type=room_type,booking_date=booking_date)
        online.save()
        return redirect('/')
    else:
        return render(request,'hotel/onlinebooking.html')

form used:

 <form action="/onlinebooking/" method="post">
                                        {% csrf_token %}
                                        <div class="text-primary">
                                            <div class="form-row">
                                                <div class="form-group col-md-6">
                                                    <label for="inputEmail4">Name</label>
                                                    <input type="text" class="form-control" id="inputEmail4" name="Name" required>
                                                </div>
                                                <!-- <div class="form-group col-md-6">
                                                    <label for="lastname">Last name</label>
                                                    <input type="text" class="form-control" id="lastname"
                                                        name="lastname" required>
                                                </div> -->
                                                <div class="form-group col-md-6">
                                                    <label for="inputPassword4">Email</label>
                                                    <input type="text" class="form-control" id="inputPassword4" name="email" required>
                                                </div>
                                                <div class="form-group col-md-6">
                                                    <label for="inputPassword4">Phone no</label>
                                                    <input type="text" class="form-control" id="inputPassword4" name="phone_no" required>
                                                </div>
<div class="form-group col-md-6">
                                                    <label for="inputState">Room Type</label>
                                                    <select id="inputState" class="form-control" name="room_type">
                                                        <option selected>Standard</option>
                                                        <option>Delux</option>
                                                        <option>Premium</option>
                                                    </select>
                                                </div>
                                                <div class="form-group col-md-6">
                                                    <label for="bookingtime">Booking Date</label>
                                                    <input type="date" id="bookingtime" name="booking_date" required>
                                                </div>
<div class="text-center">
                                                    <input type="submit" value="Register" name="submit-emp" class="btn btn-primary col-sm-3 btn-user ">
                                                </div>`

I guess there is some error with my models as I can access all the entries of the user. i think its some silly mistake from my side. Please help me out here. :)

Upvotes: 0

Views: 82

Answers (1)

asd asd
asd asd

Reputation: 146

You are overriding the import name with the class name in this file itself. Try this:

from django.shortcuts import render,redirect
from . import models

def onlinebooking(request):
    if request.method == "POST":
        name = request.POST['Name']
        email = request.POST['email']
        phone_number = request.POST['phone_no']
        room_type = request.POST['room_type']
        booking_date = request.POST['booking_date']
        online = models.onlinebooking(name=name,email=email,phone_number=phone_number,room_type=room_type,booking_date=booking_date)
        online.save()
        return redirect('/')
    else:
        return render(request,'hotel/onlinebooking.html')

Upvotes: 1

Related Questions