分享
三行代码  ›  专栏  ›  技术社区  ›  Suraj

创建模型实例时django中出现TypeError

  •  -1
  • Suraj  · 技术社区  · 2 天前

    onlinebooking()获得意外的关键字参数“name”。我在点击register按钮后得到这个错误。

    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()
    

    视图.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 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>`
    

    我想我的模型有一些错误,因为我可以访问用户的所有条目。我认为这是我的一个愚蠢的错误。请帮帮我。:)

    1 回复  |  直到 2 天前
        1
  •  1
  •   asd asd    2 天前

    您正在用此文件本身中的类名重写导入名称。

    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')