RC-
RC-

Reputation: 163

Cannot upload media files in Django production to external folder

I have an issue on my Django production server. When I try to upload images, they always go to the app/media/ folder. However I want them to be uploaded to /mnt/data. In the admin panel, when I upload the image, it is always uploading in the app/media/ folder. I tried adjusting the Nginx config file and the settings.py, but I guess I am lost.

Here is my Nginx configuration:

     location /static/ {
            root /home/somthing/something/;
        }
        location /media/ {
        root /mnt/data/;
        }

and the Settings.py:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

    # Media files

    MEDIA_URL = '/media/'

    MEDIA_ROOT = (
        os.path.join(BASE_DIR, 'media')
    )

and in my model this is how I create the image:

    pictures = models.ImageField(
            upload_to='postings/',
            verbose_name=_('Posting_picture'),
            blank=True, null=True,
            validators=[validate_image],
        )

I guess following this configuration, the uploaded picture is supposed to be in mnt/data/media/postings.

The media folder on the mnt/data/ is chmod 777, I did it when I lost hope in writing/reading the folder.

Upvotes: 0

Views: 889

Answers (2)

Debendra
Debendra

Reputation: 1142

Currently you are uploading to:

MEDIA_ROOT = (
    os.path.join(BASE_DIR, 'media')
)

Basically means:

/path/to/project/media

In your case it should be:

MEDIA_ROOT = '/mnt/data'

Upvotes: 3

RC-
RC-

Reputation: 163

I finally figured it out, Debendera was right for the path, but the Nginx configuration was wrong. I changed it to :

location /media/ {
    alias /mnt/data/;
    }

and then it worked. If I am not mistaken, it was better to use alias instead of root. This is my reference: Nginx -- static file serving confusion with root & alias

Upvotes: 1

Related Questions