我正在使用django-excel来执行此功能。
这是我的view.py
def import_data(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
request.FILES['file'].save_book_to_database(
models=['contoh_upload'],
initializers=[None,0],
mapdicts=['id','nama','kelas']
)
message = "your upload has been uploaded "
return render(request, 'kastransaksi/transaksi_formupload.html', {'message': message})
else:
message = "Failed Upluad data"
return render(request, 'kastransaksi/transaksi_formupload.html', {'form': form, 'message': message})
else:
form = UploadFileForm()
return render(request, 'kastransaksi/transaksi_formupload.html', {'form': form})
这是我的models.py
class contoh_upload(models.Model):
nama = models.CharField(max_length=10, null=True, blank=True)
kelas = models.CharField(max_length=10, null=True, blank=True)
这是我的template.html
<div class="alert-success">{{ message }}</div>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<!-- <input type="file" name="excel_file"> -->
{{ form }}
<input type="submit" name="input_button" value="UPLOAD" class="btn btn-primary">
</form>
但是我收到了这个错误:
回溯:
文件 “C:\ Python27 \ LIB \站点包\ Django的1.10.4-py2.7.egg \ Django的\核心\处理器\ exception.py” 在内心 39. response = get_response(request)
文件 “C:\ Python27 \ LIB \站点包\ Django的1.10.4-py2.7.egg \ Django的\核心\处理器\ base.py” 在_legacy_get_response中 249. response = self._get_response(request)
文件 “C:\ Python27 \ LIB \站点包\ Django的1.10.4-py2.7.egg \ Django的\核心\处理器\ base.py” 在_get_response中 187. response = self.process_exception_by_middleware(e,request)
文件 “C:\ Python27 \ LIB \站点包\ Django的1.10.4-py2.7.egg \ Django的\核心\处理器\ base.py” 在_get_response中 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件“C:\ Users \ Lenovo \ OneDrive \ siswa \ transaksi_kas \ views.py”in import_data 165. models = [(contoh_upload,['nama','kelas'],None,0)],
文件“C:\ Python27 \ lib \ site-packages \ django_excel__init __。py”in save_book_to_database 61. pe.save_book_as(** params)
save_book_as中的文件“C:\ Python27 \ lib \ site-packages \ pyexcel \ core.py” 224. return sources.save_book(book,** dest_keywords)
文件“C:\ Python27 \ lib \ site-packages \ pyexcel \ sources__init __。py”in save_book 46. return _save_any(source,book)
文件“C:\ Python27 \ lib \ site-packages \ pyexcel \ sources__init __。py”in _save_any 50. source.write_data(instance)
文件“C:\ Python27 \ lib \ site-packages \ pyexcel \ sources \ database.py”中 写入数据 248. sheet.name_columns_by_row(0)
文件“C:\ Python27 \ lib \ site-packages \ pyexcel \ sheets \ sheet.py”中 name_columns_by_row 140. self .__ column_names = make_names_unique(self.row_at(row_index))
文件“C:\ Python27 \ lib \ site-packages \ pyexcel \ sheets \ matrix.py”中 row_at 162.引发IndexError(constants.MESSAGE_INDEX_OUT_OF_RANGE)
异常类型:/ transaksi / import_form / Exception值的IndexError: 指数超出范围