Django [Errno13] PermissionError while trying to upload to static folder

Viewed 202

I am having an issue using Django Admin to upload an image to my production site.

I am running Apache HTTPD server with sqlite db.

Uploading works fine when running the development server, but when I attempt to add add a new entry to my site that includes an image I am greeted with the error:

PermissionError at /admin/homepage/jumbotron/add/

I've attempted to research this error to the best of my ability but I am at a lost now.

I have checked the file permissions including setting the group and owner to http http which is the user that apache httpd server is running under.

I went ahead and temporarily gave my static folder full permissions with chmod -R 777 and restarted the httpd server, but the error still remained. I did a full system restart as well.

The current file permissions are now:

drwxrwxr-x 8 http http 4096 Jul 3 16:48 static

I have tried other methods inside my setting.py like changing STATIC_URL to the full path or removing the first slash, so it's static/ and then restarting the server and nothing; no dice.

setting.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates'), ]

SECURE_SSL_REDIRECT = True

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'homepage',
    'blog',
    'portfolio',
]



MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'blogsite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'APP_DIRS': True,
    'DIRS': TEMPLATE_DIRS,
        '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',
                'homepage.context_processors.add_navbar_data_to_base'
            ],
        },
    },
]


STATIC_URL = 'static/'


I did some tweaks to the httpd.conf file and landed on these settings after trying Require all granted under the /static folder

httpd.conf

<VirtualHost *:443>

Alias /static /srv/http/blogsite/static
    <Directory /srv/http/blogsite/static>
               Order allow,deny
         Allow from all
    </Directory>



    <Directory /srv/http/blogsite/blogsite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess blogsite python-path=/srv/http/blogsite python-home=/srv/http/django
    WSGIProcessGroup blogsite
    WSGIScriptAlias / /srv/http/blogsite/blogsite/wsgi.py

</VirtualHost>

models.py

class Jumbotron(models.Model):
    title = models.TextField(default="")
    description = models.TextField()
    display_order = models.IntegerField()
    slideimage = models.FileField(upload_to="static/homepage")
1 Answers

Solution:

After hours of research and testing, I feel completely silly.. It seems that the relative path that "upload_to" was pointing the the root of my filesystem instead of the root of my project folder.

I really thought I had checked if that was the case previously but I guess I overlooked it. No wonder it didn't have proper permission.

I'm glad to have found a solution that works. I will have to fix the model to point to the correct folder.

models.py

Change from

slideimage = models.FileField(upload_to="static/homepage")

slideimage = models.FileField(upload_to="srv/http/blogsite/static/homepage")

I don't really like this solution. It turns out there is a media_root that django looks for that I guess I deleted or perhaps never had to begin with.

So, I set the MEDIA_ROOT inside of setting.py:

setting.py

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "blogsite")

Now this is a proper solution. Relative > Absolute

Hope this helps!

Related