Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/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
Python 应为str、bytes或os.PathLike对象,而不是NoneType_Python_Django - Fatal编程技术网

Python 应为str、bytes或os.PathLike对象,而不是NoneType

Python 应为str、bytes或os.PathLike对象,而不是NoneType,python,django,Python,Django,我得到了这个代码,它应该根据登录的用户名创建一个文件夹,并将他们上传的文件保存在该文件夹中 我的问题是它抛出了这个错误代码 应为str、bytes或os.PathLike对象,而不是NoneType 我当前的代码: def user_directory_path(instance, filename): time_stamp = 'user_{0}/{1}'.format(instance.user, filename) createfolder = os.path.join('

我得到了这个代码,它应该根据登录的用户名创建一个文件夹,并将他们上传的文件保存在该文件夹中

我的问题是它抛出了这个错误代码

应为str、bytes或os.PathLike对象,而不是NoneType

我当前的代码:

def user_directory_path(instance, filename):
    time_stamp = 'user_{0}/{1}'.format(instance.user, filename)
    createfolder = os.path.join('C:/Users/MyUser/Desktop/Project/', 'Fileuploads/', time_stamp,)
    if not os.path.exists(createfolder):
        os.makedirs(createfolder)
回溯:

Environment:


Request Method: POST Request URL: http://127.0.0.1:8000/callgolem/

Django Version: 2.0.4 Python Version: 3.6.5 Installed Applications: ['django.contrib.admin',  'django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.messages',  'django.contrib.staticfiles',  'callgolem'] Installed Middleware: ['django.middleware.security.SecurityMiddleware',  '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']





File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\wqe\Desktop\GolemProject\callgolem\views.py" in index
  65.                 instance.save()

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in save
  729.                        force_update=force_update, update_fields=update_fields)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in save_base
  759.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in _save_table
  842.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in _do_insert
  880.                                using=using, raw=raw)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in _insert
  1125.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1283.             for sql, params in self.as_sql():

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  1236.                 for obj in self.query.objs

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
  1236.                 for obj in self.query.objs

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
  1235.                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in pre_save_val
  1185.         return field.pre_save(obj, add=True)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\files.py" in pre_save
  287.             file.save(file.name, file.file, save=False)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\files.py" in save
  86.         name = self.field.generate_filename(self.instance, name)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\files.py" in generate_filename
  306.         return self.storage.generate_filename(filename)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\files\storage.py" in generate_filename
  97.         dirname, filename = os.path.split(filename)

File "C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\ntpath.py" in split
  205.     p = os.fspath(p)

Exception Type: TypeError at /callgolem/ Exception Value: expected str, bytes or os.PathLike object, not NoneType
环境:
请求方法:发布请求URL:http://127.0.0.1:8000/callgolem/
Django版本:2.0.4 Python版本:3.6.5安装的应用程序:['Django.contrib.admin','Django.contrib.auth','Django.contrib.contenttypes','Django.contrib.sessions','Django.contrib.messages','Django.contrib.staticfiles','callgolem']安装的中间件:['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware''django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.AuthenticationMiddleware.AuthenticationMiddleware','django.messages.middleware.messages.MessageMiddleware','django.middleware。XFrameOptionsMiddleware']
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\core\handlers\exception.py”位于内部
35.响应=获取响应(请求)
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\core\handlers\base.py”位于\u get\u响应中
128.response=self.process\u异常\u由\u中间件(e,请求)
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\core\handlers\base.py”位于\u get\u响应中
126.response=wrapped\u callback(请求,*callback\u args,**callback\u kwargs)
索引中的文件“C:\Users\wqe\Desktop\GolemProject\callgolem\views.py”
65.instance.save()
保存中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\base.py”
729.强制更新=强制更新,更新字段=更新字段)
保存库中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\base.py”
759.updated=self.\u save\u表(原始、cls、强制插入、强制更新、使用、更新字段)
保存表格中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\base.py”
842.result=self.\u do\u insert(cls.\u base\u manager,using,fields,update\u pk,raw)
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\base.py”位于\u do\u insert中
880.使用=使用,原始=原始)
manager\u方法中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\manager.py”
82.返回getattr(self.get_queryset(),name)(*args,**kwargs)
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\query.py”位于\u insert
返回查询。获取编译器(使用=使用)。执行sql(返回id)
执行sql中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\sql\compiler.py”
1283.对于sql,self.as_sql()中的参数为:
as\U sql中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\sql\compiler.py”
1236.对于self.query.objs中的obj
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\sql\compiler.py”
1236.对于self.query.objs中的obj
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\sql\compiler.py”
1235.【字段中字段的自我准备值(字段,自我预保存值(字段,对象)】
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\sql\compiler.py”
1185.返回字段。预保存(obj,add=True)
预保存中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\fields\files.py”
287.file.save(file.name,file.file,save=False)
保存中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\fields\files.py”
86.name=self.field.generate_文件名(self.instance,name)
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\db\models\fields\files.py”位于generate\u文件名中
306.返回self.storage.generate_文件名(filename)
文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\site packages\django\core\files\storage.py”位于generate\u文件名中
97.dirname,filename=os.path.split(filename)
split中的文件“C:\Users\wqe\AppData\Local\Programs\Python\Python36-32\lib\ntpath.py”
205.p=os.fspath(p)
异常类型/callgolem处的TypeError/Exception值:应为str、bytes或os.PathLike对象,而不是NoneType

提问时,您应该发布所有相关代码。在这种情况下,您发布了一个没有任何上下文的函数,以及一个似乎与完全不同的内容相关的回溯

但是,我想我们可以猜测,该函数被指定为FileField或ImageField的
upload\u to
参数。如果是这样,那么问题是您没有从函数中返回任何内容来告诉Django将文件实际上载到何处。您需要将文件名加入到您的目录并返回:

return os.path.join(createfolder, filename)

尝试将
\uuuu init\uuuuuuuupy
文件添加到脚本文件夹。

请发布完整的回溯。使用回溯编辑的帖子。您的路径正确吗?它似乎将windows样式(
C:
)与unix样式(文件夹之间的正向斜杠
/
)混合在一起。使用
打印(createfolder)时,您是否获得了预期的路径
?您发布的代码以何种方式与错误回溯相关?它打印C:/Users/wqe/Desktop/GolemProject/Fileuploads/user\u phillip/file.zip