Search code examples
djangoamazon-web-servicesstatic-files

Serving Static files on AWS Django application


I know that this isn't the first time the question has been asked, so I'm apologizing up front. I've been working on this for a few days and still have no clue how to proceed.

I followed this tutorial to the tee: https://aws.amazon.com/getting-started/hands-on/deploy-python-application/

The website is up and running, but of course the static files won't load.

Here's where I'm at. In settings.py, I've set my STATIC_ROOT and STATIC_URL to the following:

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

I ran collectstatic and gathered all my static files into the mysite app directory. It looks like this:

-mysite
 - mysite (app)
   - static
     - base
       - base.css
   - settings.py
   - urls.py
   - wsgi.py

Unfortunately, the static files still fail to load on the website.

Here are my questions:

  1. Suppose I wanted to view the base.css text file on the web. Would I go to www.mysite.com/static/base/base.css? If no, what would the URL be? If yes, why is it not appearing given the current set up?

  2. Per the AWS tutorial, I ran edited the httpd-app.conf file to include the following

    Alias /mysite/static /opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static

What was the purpose of the edit? How does it impact how the static files are served on the site?

Any help you guys can offer on loading these static files will be a lifesaver.


Solution

  • Your urls.py file needs to be configured to serve your STATIC_URL

    urls.py

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    More on serving static files with Django