我试图在forms.py中验证上传的excel文件。这甚至可能吗?
我收到此错误消息"强制转换为Unicode:需要字符串或缓冲区,找到NoneType"
forms.py
from .parse_excel import *
class FileSurveyForm(forms.ModelForm):
file = forms.FileField()
....
def clean_file(self):
data_file = self.cleaned_data.get('file')
data = parse_file(data_file.read())
netmaskRegex = '^(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254)))$'
for order in data:
if re.match(netmaskRegex, order['netmask']) is not None:
raise ValidationError("Invalid netmask!")
return file
parse_excel.py
import xlrd
def parse_file(datafile):
workbook = xlrd.open_workbook(file_contents=datafile)
sheet = workbook.sheet_by_index(0)
START_ROW = 35
END_ROW = 60
myList = []
for row in range(START_ROW,END_ROW):
values = (sheet.row_values(row, start_colx=1, end_colx=20))
headers = ["controller", "hostname", "domain", "ip_address", "netmask", "gateway", "dns1", "dns2", "ntp1", "ntp2",
"order_name", "order_phone", "order_email",
"shipping_adress", "shipping_city", "shipping_region", "shipping_region_code", "shipping_country", "shipping_diff"]
dictionary = dict(zip(headers, values))
myList.append(dictionary)
return myList
添加了堆栈跟踪:
Environment:
Request Method: POST
Request URL: http://10.21.145.103:8000/cooking/survey/file_upload/
Django Version: 1.6.6
Python Version: 2.6.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.formtools',
'contact',
'cooking')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/views/generic/base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "/home/jeremy.kwong/mysite/cooking/views.py" in post
101. if (form.is_valid() and ingredient_form.is_valid()):
File "/usr/lib/python2.6/site-packages/django/forms/forms.py" in is_valid
129. return self.is_bound and not bool(self.errors)
File "/usr/lib/python2.6/site-packages/django/forms/forms.py" in errors
121. self.full_clean()
File "/usr/lib/python2.6/site-packages/django/forms/forms.py" in full_clean
273. self._clean_fields()
File "/usr/lib/python2.6/site-packages/django/forms/forms.py" in _clean_fields
291. value = getattr(self, 'clean_%s' % name)()
File "/home/jeremy.kwong/mysite/cooking/forms.py" in clean_file
226. data = parse_file(data_file.read())
File "/home/jeremy.kwong/mysite/cooking/parse_excel.py" in parse_file
4. workbook = xlrd.open_workbook(file_contents=datafile)
File "/usr/lib/python2.6/site-packages/xlrd/__init__.py" in open_workbook
394. f = open(filename, "rb")
Exception Type: TypeError at /cooking/survey/file_upload/
Exception Value: coercing to Unicode: need string or buffer, NoneType found
答案 0 :(得分:1)
发生此错误是因为file_contents
的{{1}}参数为空。