我想将一个表的数据导出到django的excel工作表中我已经做了同样的事情,并为它编写了代码,但是得到了fielderror

liwlm1x9  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(160)

在此处输入图像描述here is my views.py
此代码用于导出到excel工作表

def export_excel(request):
    response = HttpResponse(content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="candidates.xls"'

    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('candidates') 
    # Sheet header, first row
    row_num = 0

    font_style = xlwt.XFStyle()
    font_style.font.bold = True

    columns = ['Full Name ', 'Email', 'Notice Period (in Days)', 'Current Location', 'Expected Location', 'Current CTC (Per Annum)', 'Expected CTC (Per Annum)', 'Upload CV', ' Gender']

    for col_num in range(len(columns)):
        ws.write(row_num, col_num, columns[col_num], font_style)

    # Sheet body, remaining rows
    font_style = xlwt.XFStyle()

    rows = ApplyForm.objects.all().values_list('full_name', 'email', 'noticeperiod', 'preferredlocation', ' expectedlocation', 'currentctc', ' expectedctc', ' cv', 'gender' )
    for row in rows:
        row_num += 1
        for col_num in range(len(row)):
            ws.write(row_num, col_num, row[col_num], font_style)

    wb.save(response)
    return response

这是用于填写表单并提交到databasein views.py的代码

def applyform(request):
    data = ApplyForm.objects.all()
    print(data)

    if request.method == "POST":
        full_name = request.POST['full_name']
        email = request.POST['email']
        noticeperiod = request.POST['noticeperiod']
        preferredlocation = request.POST['preferredlocation']
        expectedlocation = request.POST['expectedlocation']
        currentctc = request.POST['currentctc']
        expectedctc= request.POST['expectedctc']
        gender = request.POST['gender']
        cv = request.FILES['cv']

        ins = ApplyForm(full_name = full_name, email = email,  noticeperiod = noticeperiod, preferredlocation = preferredlocation, expectedlocation= expectedlocation, currentctc =currentctc, expectedctc = expectedctc, gender = gender, cv = cv)
        ins.save()
        print("the data has been written to the db")

这是我的模特

class ApplyForm(models.Model):
    full_name = models.CharField(max_length=300)
    email = models.EmailField(max_length=300)
    noticeperiod = models.CharField(max_length = 300)
    preferredlocation = models.CharField(max_length=300)
    expectedlocation = models.CharField(max_length=300)
    currentctc = models.CharField(max_length=300)
    expectedctc = models.CharField(max_length=300)
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
    cv = models.FileField(upload_to='')

我得到了一个fielderror的错误,但是所有的字段都是正确的,我检查了很多次试图解决这个问题,我只想在excel表中导出我的表,其中所有的数据都来自数据库。我试了很长时间,但还是没能解决。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题