Мой план состоит в том, чтобы позволить пользователю загружать файл excel, после загрузки я буду отображать редактируемую форму, содержащую содержимое загруженного excel, как только пользователь подтвердит правильность ввода, он/она нажал кнопку сохранения, и эти элементы сохраняются в некоторой модели.
Для этого я написал это представление и форму:
форма:
IMPORT_FILE_TYPES = ['.xls', ]
class XlsInputForm(forms.Form):
input_excel = forms.FileField(required= True, label= u"Upload the Excel file to import to the system.")
def clean_input_excel(self):
input_excel = self.cleaned_data['input_excel']
extension = os.path.splitext( input_excel.name )[1]
if not (extension in IMPORT_FILE_TYPES):
raise forms.ValidationError( u'%s is not a valid excel file. Please make sure your input file is an excel file (Excel 2007 is NOT supported.' % extension )
else:
return input_excel
Вид:
def import_excel_view(request):
if request.method == 'POST':
form = XlsInputForm(request.POST, request.FILES)
if form.is_valid():
input_excel = request.FILES['input_excel']
# I need to open this input_excel with input_excel.open_workbook()
return render_to_response('import_excel.html', {'rows': rows})
else:
form = XlsInputForm()
return render_to_response('import_excel.html', {'form': form})
Как вы можете видеть в # I need to open this input_excel with input_excel.open_workbook()
мне нужно читать из памяти, но open_workbook
читает из файла, не сохраняя этот вход где-то, как его прочитать?