简体   繁体   English

绝对路径? models.FileFIeld 的 `upload_to`

[英]absolute path? `upload_to` of models.FileFIeld

I have FileField in models.py我在models.pyFileField

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/它适用于本地(mac),并且文件存储在/Users/whitebear/myproj/_mat/

However I do the same thing on server (Ubuntu 20, using ENGINX-unit)但是我在服务器上做同样的事情(Ubuntu 20,使用 ENGINX-unit)

It shows the error PermissionError: [Errno 13] Permission denied: '/_mat'它显示错误PermissionError: [Errno 13] Permission denied: '/_mat'

But, /var/www/html/myproj/_mat/ permission is 777但是, /var/www/html/myproj/_mat/权限是 777

So I guess, somehow it trys to make /_mat as absolute path ..???所以我想,它以某种方式试图将/_mat作为绝对路径 ..???

If I set upload_to like this ,如果我像这样设置upload_to

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'

The MEDIA_ROOT setting [Django-doc] specifies the: MEDIA_ROOT设置 [Django-doc]指定:

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.如果MEDIA_ROOT/ ,那么它会将这些存储在服务器的/_mat/目录中。

You thus can set the MEDIA_ROOT setting to:因此,您可以将MEDIA_ROOT设置设置为:

# settings.py

# ⋮,

MEDIA_ROOT = '/var/www/html/myproj/'

# ⋮

permission denied i think come from ubuntu file system it self so you can run权限被拒绝我认为来自 ubuntu 文件系统它自己所以你可以运行

sudo chmod -R 755 /var/www/html/myproj/_mat/须藤 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并且最好让 Nginx 的用户与 gunicorn 相同,并让该用户访问您的媒体,如下所示让您的用户是 user1

sudo chown -R user1:user1 /var/www/html/myproj/_mat/须藤 chown -R user1:user1 /var/www/html/myproj/_mat/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM