我是编程新手。我上传了一个excel,并希望将数据保存到数据库中。但是当我尝试通过使用stackoverflow进行检查来解决它时,我遇到了各种错误。这是我的代码
我的观点:
def createSchool(request):
if request.method == 'POST' and request.FILES['excel']:
localvar= kidDetailsForm(request.POST)
myfile = request.FILES['excel']
book = xlrd.open_workbook(myfile.name)
sheet = book.sheet_by_index(0)
for r in range(1, sheet.nrows):
if localvar.is_valid():
temp = localvar.save(commit = False)
temp.childname = sheet.cell(r,0).value
temp.dob = sheet.cell(r,1).value
temp.sex = sheet.cell(r,2).value
temp.save()
return render(request, 'littleStar/createSchool.html')
return render(request, 'littleStar/createSchool.html')
我的表格:
class kidDetailsForm(forms.Form):
class Meta():
model = kidDetails
fields = ("childname","dob","sex")
我的模特:
class kidDetails(models.Model):
childname= models.CharField(max_length=245)
sex = models.CharField(max_length=245)
dob = models.DateField(max_length=245)
def __str__(self):
return self.childname
但是当我打印表格时。我看到了所需的错误。有没有办法绕过表格。
我想上传excel并将数据保存到数据库。任何帮助都会很棒,非常感谢,谢谢。