Search code examples
pythondjangomacosapacheosx-elcapitan

OS X Django/Apache app not serving media files, works using python shell


I have a simple Django app that displays a bunch of thumbnails to videos. The first thing the app does is finds all of the thumbnail files in the MEDIA_ROOT. Everything else trickles down from that. I've got apache serving my site correctly, however it can't find the files in my media_root.

views.py:

def index(request): 
    def walk_dir_tree(path, file_type):
        return[os.path.basename(x) for x in glob.glob(path + file_type,
                                                      recursive=True)]   
    # Gets all .jpgs in media_root
    all_thumb_files = sorted(walk_dir_tree(
        settings.MEDIA_ROOT + 'thumbnails/**/*', '.jpg'), reverse=True)
...
return HttpResponse(template.render(context, request))

relevant info from settings.py:

# URLs
STATIC_URL = '/static/'
MEDIA_URL = '/media/'

# Root Dirs:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# Dev
MEDIA_ROOT = '/Users/user/Documents/temp/secureDash/'

http-vhosts.conf:

<VirtualHost *:80>

    LogLevel debug

    ServerName securedash
    ServerAdmin <email>


    Alias /static /Users/user/icloud/projects/secureDash/static
    <Directory /Users/user/icloud/projects/secureDash/static>
        Require all granted
    </Directory>

    Alias /media /Users/user/Documents/temp/secureDash
    <Directory /Users/user/Documents/temp/secureDash>
        Require all granted
    </Directory>

    <Directory /Users/user/icloud/projects/secureDash/secureDash>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess securedash python-path=/Users/user/icloud/projects/secureDash/secureDash python-home=/Users/user/Documents/VirtualEnvs/securedash
    WSGIProcessGroup securedash
    WSGIScriptAlias / /Users/user/icloud/projects/secureDash/secureDash/wsgi.py
</VirtualHost>

As a test to ensure that the path to my media was correct, I wrote a quick script and ran it in the python3 shell:

MEDIA_ROOT = '/Users/user/Documents/temp/secureDash/'

def walk_dir_tree(path, file_type):
    return[os.path.basename(x) for x in glob.glob(path + file_type,
                                                  recursive=True)]

all_thumb_files = sorted(walk_dir_tree(
    MEDIA_ROOT + 'thumbnails/**/*', '.jpg'), reverse=True)


# prints a list of all jpgs
print(walk_dir_tree('/Users/user/Documents/temp/secureDash/thumbnails/**/*', '.jpg')) 
# also prints a list of all jpgs
print(all_thumb_files) 

There are no errors logged anywhere. It just returns an empty list every time. I figure this is likely an issue with my settings.py or apache configuration but I've tried numerous combinations and nothing has worked.

I have this exact configuration working on my "production" server on my raspberry pi. I'm just trying to set up a dev environment that more closely matches the production one. Only differences are the OSs and MEDIA_ROOT locations.

Django 1.10 | OSX 10.11 El Capitan | Apache 2.4


Solution

  • This ended up being a permission issue with /Users/user/Documents/temp/secureDash/, but what helped me work that out was the suggestion by @mithuntnt to glob a different folder. So I went back and globbed /Users/user/Documents which did return results. Then adding /temp which returned results. Then /secureDash which did not. I tried a different directory in /temp that was at the same level as /secureDash and that worked. So I checked the permissions on /secureDash and it was:

    17276808 drwx------@  6 user  _www                           204B Mar 29 15:04 secureDash
    

    So I ran chmod -R 755 /Users/user/Documents/temp/secureDash/

    And now the thumbnails are loading. Shouldn't I have seen file permission errors in the apache logs being that was the issue? I had LogLevel debug in http-vhosts.conf. What is a good LogLevel setting for a development environment?