Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ubuntu 16.04上的Django导入导出引发JSON错误_Django_Ubuntu 16.04_Django Import Export - Fatal编程技术网

Ubuntu 16.04上的Django导入导出引发JSON错误

Ubuntu 16.04上的Django导入导出引发JSON错误,django,ubuntu-16.04,django-import-export,Django,Ubuntu 16.04,Django Import Export,我正在尝试将*.xslx文件从django应用程序上载到模型。在我将应用程序部署到linux虚拟机(Ubuntu 16.04)上之前,一切都很顺利 它会引发以下错误: JSON对象必须是str,而不是“bytes” 这是my views.py中的代码: if request.method == "POST": try: form = UploadFileForm(request.POST, request.FILES["excel_file"])

我正在尝试将*.xslx文件从django应用程序上载到模型。在我将应用程序部署到linux虚拟机(Ubuntu 16.04)上之前,一切都很顺利

它会引发以下错误: JSON对象必须是str,而不是“bytes”

这是my views.py中的代码:

    if request.method == "POST":
        try:
            form = UploadFileForm(request.POST, request.FILES["excel_file"])
            file =  request.FILES['excel_file']
            Import_Resource = resources.modelresource_factory(model=item)()
            dataset =  tablib.Dataset()
            if not file.name.endswith('.xlsx'):
                messages.error(request, 'This is not a excel file!')
            imported_data = dataset.load(file.read())
我尝试解码dataset对象(如utf-8),但随后收到错误,即它无法解码某个位置的字符-我猜这是因为我尝试导入的文件内容中有一些特殊的拉丁字符。 如果我使用拉丁语-1,它将返回以下错误:
类'keyrerror'>'p'

这是生成上述错误的已审核代码:

def upload(request):
    if request.method == "POST":
        try:
            form = UploadFileForm(request.POST, request.FILES["excel_file"])
            file =  request.FILES['excel_file']
            Import_Resource = resources.modelresource_factory(model=item)()
            dataset =  tablib.Dataset()
            if not file.name.endswith('.xlsx'):
                messages.error(request, 'This is not a excel file!')
            imported_data = dataset.load(file.read().decode('latin-1'))

你们对如何解决这个问题有什么想法吗?

错误的完整回溯是什么?什么是资源。modelresource\u工厂,什么是tablib?您使用的是什么版本的Python?我安装了Python3.7,但现在正试图在VM上将其配置为要使用的主版本。我的Ubuntu虚拟机上的默认python似乎是2.7.3(可能),这就是问题所在。如果它似乎缺少文件格式类型:imported_data=dataset.load(file.read(),format='xlsx')现在可以工作了!:)