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

标签 python django

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

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

expected str, bytes or os.PathLike object, not 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

最佳答案

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

关于python - 预期的 str、bytes 或 os.PathLike 对象,而不是 NoneType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50585142/

相关文章:

python - TA-Lib numpy "AssertionError: real is not double"

python - 连接字典

python - 合并到不同的词典

python - 使用 Django 的 ORM 和 Django Rest Framework 序列化嵌套关系的查询集的正确方法?

django - 在 pyCharm 中配置没有迁移的 django 测试

python - 如何在 Django 模板中使用 'is not None'?

mysql - 按一行中的相似项目对查询进行分组

python - 使用 mypy,我如何键入注释一个装饰器来装饰接受某个东西的子类的函数?

python - Geany 无法执行 Python

python - Django mysql 数据库凭据 read_default_file 给出错误