TypeError:kitty()获得了意外的关键字参数'startdate'

时间:2019-10-29 04:42:34

标签: django django-templates django-views

我遇到此错误-TypeError:kitty()得到了一个我毫无头绪的意外关键字参数'startdate'。可能是日期格式问题。

模型

from django.db import models
import datetime
from datetime import datetime
from multiselectfield import MultiSelectField

class kitty(models.Model):
    company = models.CharField(max_length=3)
    code = models.CharField(max_length=6)
    type = models.CharField(max_length=10)
    name = models.CharField(max_length=100)
    startdate = models.DateField
    durinmonths = models.IntegerField()
    enddate = models.DateField
    totalmembers = models.IntegerField()
    totalamount = models.FloatField()
    installmentamount = models.FloatField()
    status = models.CharField(max_length=1)
    creator = models.CharField(max_length=20)
    cretime = models.DateTimeField(default=datetime.now)
    updator = models.CharField(max_length=20)
    updtime = models.DateTimeField(default=datetime.now, blank = True)

查看

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
import datetime
from .models import kitty
from django.db import transaction
from django.contrib import messages
import re

def index(request):
    return render(request, 'kitty/index.html')


def kitty_save(request):                
    if request.method == 'POST':                    
        company = "1"
        code1 = request.POST['code']  
        type1 = request.POST['type']
        name = request.POST['name']
        startdate = request.POST['startdate']
        durinmonths = request.POST['durinmonths']
        enddate = request.POST['enddate']
        totalmembers = request.POST['totalmembers']
        totalamount = request.POST['totalamount']
        installmentamount = request.POST['installmentamount']
        print(startdate)
        status = "A"
        creator = request.user

        kitty1 = kitty(company=company, code=code1, type=type1,name=name, startdate=startdate, durinmonths=durinmonths,
                             enddate=enddate,totalmembers=totalmembers,totalamount=totalamount,installmentamount=installmentamount,
                             status=status, creator=creator)
        kitty1.save()
        transaction.commit()
        messages.success(request, 'Kitty Added')
        return HttpResponseRedirect(request.path_info)
    else:
        return render(request, 'kitty/kitty.html')

URL

    from django.urls import path
    from . import views

    urlpatterns = [
        path('',views.index,name='index'),
        path('kitty',views.kitty_save,name='kitty')]

HTML

<form class="form-signin" action="{% url 'kitty' %}"  method="POST">
  {% csrf_token %}
  <h1 class="h3 mb-3 font-weight-normal redc">Kitty Creation</h1>
  <div class="mb-3">
    <input type="text" name="code" id="code" class="form-control-sm center-block" placeholder="Kitty Code"
      required autofocus >
  </div>
  <div class="mb-3">
    <input type="text" name="name" id="name" class="form-control-sm center-block" placeholder="Name"
      required autofocus>
  </div>
  <div class="mb-3">
    <select class="custom-select center-block" name="type" id="type" required>
      <option value="">Choose Type of Kitty...</option>
      <option>Diamond</option>
      <option>Gold</option>
    </select>
    <div class="invalid-feedback">
      Please select a valid Existing Customer.
    </div>
  </div>
  <input type="date" class="form-control-sm center-block" name="startdate" id="startdate"
    placeholder="Start Date" required>
  <div class="invalid-feedback">
    Please enter Start Date.
  </div>

1 个答案:

答案 0 :(得分:1)

您需要调用字段类型-现在Django不会将startdate用作字段,因为您将其设置为字段类,而不是字段类的实例。

startdate = models.DateField更改为startdate = models.DateField(),运行./manage.py makemigrations创建迁移以捕获更改并重试。

相关问题