简体   繁体   English

预期的 str、bytes 或 os.PathLike 对象,而不是 NoneType

[英]expected str, bytes or os.PathLike object, not NoneType

I got this code which is supposed to create a folder based on the logged in users name and save the file they upload inside that folder.我得到了这段代码,它应该根据登录的用户名创建一个文件夹,并将他们上传的文件保存在该文件夹中。

My problem is that it throws this error code我的问题是它抛出这个错误代码

expected str, bytes or os.PathLike object, not NoneType预期的 str、bytes 或 os.PathLike 对象,而不是 NoneType

My current code:我目前的代码:

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)

Traceback:追溯:

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

尝试将__init__.py文件添加到脚本文件夹中。

When asking a question, you should post all the relevant code.当提出问题时,您应该发布所有相关代码。 In this case, you've posted a function, without any context, and a traceback which appears to relate to something completely different.在这种情况下,您发布了一个没有任何上下文的函数,以及一个似乎与完全不同的事物相关的回溯。

However, I think we can guess that this function is assigned as the upload_to parameter of a FileField or ImageField.但是,我我们可以猜测这个函数被分配为 FileField 或 ImageField 的upload_to参数。 If so, then the problem is that you do not return anything from your function to tell Django where to actually upload the file to.如果是这样,那么问题是您没有从函数中返回任何内容来告诉 Django 将文件实际上传到的位置。 You need to join the filename to your directory and return that:您需要将文件名加入您的目录并返回:

return os.path.join(createfolder, filename)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django TypeError:预期的 str、bytes 或 os.PathLike object,不是 NoneType - Django TypeError: Expected str, bytes or os.PathLike object, not NoneType 预期的 str、bytes 或 os.PathLike object,而不是 NoneType(tkinter 错误) - expected str, bytes or os.PathLike object, not NoneType (tkinter error) pyqtdeploy : TypeError: 预期的 str、bytes 或 os.PathLike 对象,而不是 NoneType - pyqtdeploy : TypeError: expected str, bytes or os.PathLike object, not NoneType docker-compose/django TypeError:预期的 str、字节或 os.PathLike 对象,而不是 NoneType - docker-compose/django TypeError: expected str, bytes or os.PathLike object, not NoneType 类型错误:运行 mrjob 时预期的 str、字节或 os.PathLike object,而不是 NoneType - TypeError: expected str, bytes or os.PathLike object, not NoneType when running mrjob 使用 pyinstaller 生成 exe 文件的错误 - typeerror: expected str, bytes or os.PathLike object, not NoneType - An error for generating an exe file using pyinstaller - typeerror: expected str, bytes or os.PathLike object, not NoneType 使用 py2app 预期的 str、bytes 或 os.PathLike 对象创建可执行文件时出错,而不是 NoneType - Error creating executable with py2app expected str, bytes or os.PathLike object, not NoneType 有关TypeError的错误:预期的str,字节或os.PathLike对象,而不是NoneType - An error about TypeError: expected str, bytes or os.PathLike object, not NoneType makemigration 导致错误“TypeError:预期的 str、字节或 os.PathLike 对象,而不是 NoneType” - makemigration causing error "TypeError: expected str, bytes or os.PathLike object, not NoneType" 类型错误:尝试安装 muonic 时应为 str、字节或 os.PathLike object,而不是 NoneType - TypeError: expected str, bytes or os.PathLike object, not NoneType while trying to install muonic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM