如何在smart_str writer.writerow?中处理非类型,这是我将数据导出到excel的方式
我使用了smart_str这是我的完整代码,我希望这将澄清或理解我的问题,我希望这将解决问题
我这里有三个模型
学生记录 学生\用户(字段)类名学生档案 在StudentProfile下,我有另一个foriegnkey家长用户(ParentsProfile)
def downloadcv(request):
response = HttpResponse(content_type='text/csv')
# decide the file name
response['Content-Disposition'] = 'attachment; filename="StudentEnrollmentRecord.csv"'
writer = csv.writer(response, csv.excel)
response.write(u'\ufeff'.encode('utf8'))
# write the headers
writer.writerow([
smart_str(u"students ID"),
smart_str(u"Fullname"),
smart_str(u"Courses"),
smart_str(u"Strands"),
smart_str(u"Section"),
smart_str(u"Payment_Type"),
smart_str(u"Discount_Type"),
smart_str(u"Education_Levels"),
smart_str(u"Semester"),
smart_str(u"ESC"),
smart_str(u"Student_Types"),
smart_str(u"Last_School_Attended"),
smart_str(u"Address_OF_School_Attended"),
smart_str(u"GWA"),
smart_str(u"School_Year"),
smart_str(u"Birthday"),
smart_str(u"Citizenship"),
smart_str(u"Religion"),
smart_str(u"Place_Of_Birth"),
smart_str(u"Telephone_Number"),
smart_str(u"Gender"),
smart_str(u"Home_Address"),
smart_str(u"Fathers_Fullname"),
smart_str(u"Fathers_Occupation"),
smart_str(u"Fathers Company"),
smart_str(u"Fathers_Gmail"),
smart_str(u"Fathers_Educational_Attainment"),
smart_str(u"Fathers_Contact_Number"),
smart_str(u"Fathers Landline"),
smart_str(u"Mothers_Fullname"),
smart_str(u"Mothers_Occupation"),
smart_str(u"Mothers Company"),
smart_str(u"Mothers_Gmail"),
smart_str(u"Mothers_Educational_Attainment"),
smart_str(u"Mothers_Contact_Number"),
smart_str(u"Mothers_Landline"),
smart_str(u"Family_Total_Income"),
smart_str(u"Number_Of_Siblings"),
smart_str(u"Family_Status"),
smart_str(u"Guardians_Fullname"),
smart_str(u"Guardians_Address"),
smart_str(u"Contact_Number"),
])
# get data from database or from text file....
yearid = request.POST.get("yearid")
print("yearid: ", yearid)
reports = StudentsEnrollmentRecord.objects.filter(School_Year=yearid).order_by('Education_Levels','-Date_Time')
print(reports)
for report in reports:
writer.writerow([
smart_str(report.id),
smart_str(report.Student_Users),
smart_str(report.Courses),
smart_str(report.strands),
smart_str(report.Section),
smart_str(report.Payment_Type),
smart_str(report.Discount_Type),
smart_str(report.Education_Levels),
smart_str(report.Semester),
smart_str(report.ESC),
smart_str(report.Student_Types),
smart_str(report.Last_School_Attended),
smart_str(report.Address_OF_School_Attended),
smart_str(report.GWA),
smart_str(report.School_Year.Description),
smart_str(report.Student_Users.Birthday) or "None",
smart_str(report.Student_Users.Citizenship) or "None",
smart_str(report.Student_Users.Religions) or "None",
smart_str(report.Student_Users.Place_Of_Birth) or "None",
smart_str(report.Student_Users.Landline) or "None",
smart_str(report.Student_Users.Gender) or "None",
smart_str(report.Student_Users.Unit_Number_Street + report.Student_Users.Barangay + report.Student_Users.City + report.Student_Users.AddressLine1) or "None",
smart_str(report.Student_Users.Parent_Users.Fathers_Lastname + report.Student_Users.Parent_Users.Fathers_Firstname + report.Student_Users.Parent_Users.Fathers_Middle_Initial) or "None",
smart_str(report.Student_Users.Parent_Users.Father_Occupation) or "None",
smart_str(report.Student_Users.Parent_Users.Father_Company_Employed) or "None",
smart_str(report.Student_Users.Parent_Users.Father_Email) or "None",
smart_str(report.Student_Users.Parent_Users.Educational_AttainmentID_Father.Description) or "None",
smart_str(report.Student_Users.Parent_Users.Father_MobileNo) or "None",
smart_str(report.Student_Users.Parent_Users.Father_Landline) or "None",
smart_str(report.Student_Users.Parent_Users.Mother_Maiden_LastName + report.Student_Users.Parent_Users.Mother_FirstName + report.Student_Users.Parent_Users.Mother_Middle_Initial) or "None",
smart_str(report.Student_Users.Parent_Users.Mother_Occupation) or "None",
smart_str(report.Student_Users.Parent_Users.Mother_Company_Employed) or "None",
smart_str(report.Student_Users.Parent_Users.Mother_Email) or "None",
smart_str(report.Student_Users.Parent_Users.Educational_AttainmentID_Mother.Description) or "None",
smart_str(report.Student_Users.Parent_Users.Mother_MobileNo) or "None",
smart_str(report.Student_Users.Parent_Users.Mother_Landline) or "None",
smart_str(report.Student_Users.Parent_Users.Family_Total_Income.Description) or "None",
smart_str(report.Student_Users.Parent_Users.numberOfSiblings) or "None",
smart_str(report.Student_Users.Parent_Users.Family_Status.Description) or "None",
smart_str(report.Student_Users.Parent_Users.Guardian_LastName + report.Student_Users.Parent_Users.Guardian_FirstName + report.Student_Users.Parent_Users.Guardian_Middle_Initial) or "None",
smart_str(report.Student_Users.Unit_Number_Street + report.Student_Users.Barangay + report.Student_Users.City + report.Student_Users.AddressLine1) or "None",
smart_str(report.Student_Users.Parent_Users.Guardian_MobileNo) or "None",
])
return response if response is not None else 'None'
这就是错误所在
完全回溯
Traceback:
File "/home/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/home/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/home/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/unidaweb/unidaproject/Homepage/views.py" in downloadcv
389. smart_str(report.Student_Users.Parent_Users.Guardian_LastName + report.Student_Users.Parent_Users.Guardian_FirstName + report.Student_Users.Parent_Users.Guardian_Middle_Initial) or "None",
Exception Type: TypeError at /downloadcv/
Exception Value: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
正如@jujule和@May Yie所说,实现它的最好方法是重构
smart_str
函数,您可以尝试以下方法:重要的一点是,您将停止错误在使用
smart_str
函数的任何视图中传播如果您无法控制
smart_str
,您可以创建一个名为super_smart_str(mystr)
的函数,您可以执行以下操作:然后您可以自定义此选项:
最后,您将处理连接:
就这样
您可以使用
smart_str(u"Citizenship") or "unknown"
来拥有回退字符串相关问题 更多 >
编程相关推荐