使用Nginx制作期间的Django Media Files?

时间:2019-07-23 12:14:24

标签: django

在制作过程中如何提供媒体文件?

在nginx中,我有:

upstream app_server {
    server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
    # listen 80 default_server;
    # listen [::]:80 default_server ipv6only=on;
    listen 443 ssl;

    server_name tweetz.co;

    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static;
    }

client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

# Your Django project's media files - amend as required
location /media  {
    alias /home/django/django_project/django_project/media;
}

# your Django project's static files - amend as required
location /static {
    alias /home/django/django_project/django_project/static;
}

# Proxy the static assests for the Django Admin panel
location /static/admin {
   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_buffering off;

        proxy_pass http://app_server;
}

}
` `# Proxy the static assests for the Django Admin panel
    location /static/admin {
   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_buffering off;

        proxy_pass http://app_server;
}

}
server{
    listen 80;
    server_name mywebsite.com;
    return 301 https://$host$request_uri;
}

不起作用。我需要更改其他内容吗?


在我的文件下面:

models.py

image       = models.ImageField(upload_to='media',blank=True)

urls.py

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py

MEDIA_ROOT = '/home/django/django_project/static-serve/media/'
MEDIA_URL = '/media/'
server {
        '/home/django/django_project/static-serve/media/image.jpg'
        location /media {
            autoindex on;
            alias /home/django/django_project/static-serve/media/;
        }
    }

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

模板

<img src="{{ object.profile.image.url }}" class="w3-circle" style="height:106px;width:106px" alt="Avatar">

浏览器中的图像源是:mysite / media / media / myimage.jpg

图像已保存:home / django / django_project / media / media / myimage.jpg

谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

您不应 在路径前加上斜杠,因为这意味着您要将文件上传到目录 root 中名为media的目录中文件系统。

例如,您可以将其上传到:

    image = models.ImageField(upload_to='image/',blank=True)

但是,如果您省略upload_to=... parameter [Django-doc],则Django会将其简单地上传到MEDIA_ROOT [Django-doc]