-1

My code throws me an exception but Googling hasnt helped me. 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'

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'
Phillip
  • 295
  • 1
  • 3
  • 12
  • Have you checked `filename`'s value ? (By printing it for instance) – gogaz May 29 '18 at 14:50
  • The first line in my console log shows the file name which is correct. – Phillip May 29 '18 at 14:51
  • Nothing in your function would generate an internal server error. What uses `user_directory_path`? Note that `os.path.join` isn't creating an absolute path; you probably want `/home` as the first argument. – chepner May 29 '18 at 14:54
  • Cant upload all the code since it requires me to type more – Phillip May 29 '18 at 15:07
  • The model I use for my upload field upload = models.FileField(upload_to=user_directory_path) and the error occures when my code tries to execute instance.save in my views.py – Phillip May 29 '18 at 15:08

1 Answers1

2

Your user_directory_path function doesn't return anything (or, more exactly, it's dioesn't explicitely return anything so it implicitely returns None). It has to return the full file path (relative to your settings.MEDIA_ROOT) including the filename:

upload_to may also be a callable, such as a function. 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

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

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • 99% sure this is the correct answer. Now I gotta figure out how to do it with my current code. Thanks – Phillip May 29 '18 at 15:30