I have FileField
in models.py
class DocFile(models.Model):
document = models.FileField(upload_to='_mat/')
It works well for local(mac),and the file is stored under /Users/whitebear/myproj/_mat/
However I do the same thing on server (Ubuntu 20, using ENGINX-unit)
It shows the error PermissionError: [Errno 13] Permission denied: '/_mat'
But, /var/www/html/myproj/_mat/
permission is 777
So I guess, somehow it trys to make /_mat
as absolute path ..???
If I set upload_to
like this ,
document = models.FileField(upload_to='/var/www/html/myproj_mat/')
It says to use 'relative path'
These are error stack trace.
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/var/www/html/aicomposer/current/defapp/views.py", line 350, in post
entry.save()
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/base.py", line 763, in save_base
updated = self._save_table(
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/base.py", line 906, in _do_insert
return manager._insert(
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1415, in execute_sql
for sql, params in self.as_sql():
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1358, in as_sql
value_rows = [
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1310, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/fields/files.py", line 302, in pre_save
file.save(file.name, file.file, save=False)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/db/models/fields/files.py", line 89, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/core/files/storage.py", line 54, in save
return self._save(name, content)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/site-packages/django/core/files/storage.py", line 255, in _save
os.makedirs(directory, exist_ok=True)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/os.py", line 213, in makedirs
makedirs(head, exist_ok=exist_ok)
File "/home/ubuntu/anaconda3/envs/aiwave/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/_mat'
CodePudding user response:
The MEDIA_ROOT
setting [Django-doc] specifies the:
Absolute filesystem path to the directory that will hold user-uploaded files.
You thus should specify with this setting where you will store the media files. If MEDIA_ROOT
is thus /
, then it will store these in the /_mat/
directory of the server.
You thus can set the MEDIA_ROOT
setting to:
# settings.py
# ⋮,
MEDIA_ROOT = '/var/www/html/myproj/'
# ⋮
CodePudding user response:
permission denied i think come from ubuntu file system it self so you can run
sudo chmod -R 755 /var/www/html/myproj/_mat/
and it is better also to make user of Nginx is same as gunicorn and give this user access to your media as follow let your user is user1
sudo chown -R user1:user1 /var/www/html/myproj/_mat/