简体   繁体   English

异常值:“ NoneType”对象没有属性“ rfind”

[英]Exception Value: 'NoneType' object has no attribute 'rfind'

My code throws me an exception but Googling hasnt helped me. 我的代码引发了异常,但Google并未帮助我。 I did an error check on it possibly could be it getting the wrong path, but it prints the correct one out to the console. 我对其进行了错误检查,可能是路径错误,但是它向控制台输出了正确的输出。

Exception Value: 异常值:

'NoneType' object has no attribute 'rfind' 'NoneType'对象没有属性'rfind'

def user_directory_path(instance, filename):
    time_stamp = 'user_{0}/{1}'.format(instance.user, filename)
    createfolder = os.path.join('home', 'ttt', 'Desktop', 'GolemProject', 'Fileuploads', time_stamp,)
    print(createfolder)
    if not os.path.exists(createfolder):
        os.makedirs(createfolder)

From the console: 从控制台:

home/ttt/Desktop/GolemProject/Fileuploads/user_wqe/vbbfgsfdgfds.zip
Internal Server Error: /callgolem/
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ttt/Desktop/GolemProject/callgolem/views.py", line 65, in index
    instance.save()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1283, in execute_sql
    for sql, params in self.as_sql():
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1236, in as_sql
    for obj in self.query.objs
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1236, in <listcomp>
    for obj in self.query.objs
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1235, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1185, in pre_save_val
    return field.pre_save(obj, add=True)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 287, in pre_save
    file.save(file.name, file.file, save=False)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 86, in save
    name = self.field.generate_filename(self.instance, name)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 306, in generate_filename
    return self.storage.generate_filename(filename)
  File "/usr/local/lib/python3.5/dist-packages/django/core/files/storage.py", line 97, in generate_filename
    dirname, filename = os.path.split(filename)
  File "/usr/lib/python3.5/posixpath.py", line 103, in split
    i = p.rfind(sep) + 1
AttributeError: 'NoneType' object has no attribute 'rfind'

Your user_directory_path function doesn't return anything (or, more exactly, it's dioesn't explicitely return anything so it implicitely returns None ). 您的user_directory_path函数不返回任何内容(或更确切地说,它不显式返回任何内容,因此它隐式返回None )。 It has to return the full file path (relative to your settings.MEDIA_ROOT ) including the filename: 它必须返回完整的文件路径(相对于settings.MEDIA_ROOT ),包括文件名:

upload_to may also be a callable, such as a function. upload_to也可以是可调用的,例如函数。 This will be called to obtain the upload path, including the filename. 这将被调用以获得上载路径,包括文件名。 This callable must accept two arguments and return a Unix-style path (with forward slashes) to be passed along to the storage system 此可调用对象必须接受两个参数,并返回要传递给存储系统的Unix样式的路径(带有正斜杠)。

https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.FileField.upload_to https://docs.djangoproject.com/zh-CN/2.0/ref/models/fields/#django.db.models.FileField.upload_to

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

相关问题 Python:AttributeError:'NoneType'对象没有属性'rfind' - Python: AttributeError: 'NoneType' object has no attribute 'rfind' 异常值:“ NoneType”对象没有属性“ endswith” - Exception Value: 'NoneType' object has no attribute 'endswith' AttributeError:“列表”对象没有属性“ rfind” - AttributeError: 'list' object has no attribute 'rfind' 触发异常:“NoneType”对象没有“app”属性 - Exception triggered: 'NoneType' object has no attribute 'app' AttributeError: &#39;list&#39; 对象没有使用 petsc4py 的属性 &#39;rfind&#39; - AttributeError: 'list' object has no attribute 'rfind' using petsc4py 如何修复“ AttributeError:&#39;RDD&#39;对象没有属性&#39;rfind&#39;”? - How to fix “AttributeError: 'RDD' object has no attribute 'rfind'”? 发送带有附件的电子邮件时,“ InMemoryUploadedFile”对象没有属性“ rfind” - 'InMemoryUploadedFile' object has no attribute 'rfind' when sending email with attachment NoneType 对象没有属性 - NoneType object has no attribute makedirs()给出AttributeError:&#39;int&#39;对象没有属性&#39;rfind&#39; - makedirs() gives AttributeError: 'int' object has no attribute 'rfind' 异常值:&#39;NoneType&#39; 对象没有属性 &#39;add&#39;,类别名称不显示 Django - Exception Value: 'NoneType' object has no attribute 'add', Categories name dont show up Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM