0

I've build a Django site on a Digital Ocean droplet but the static files seem to be inaccessible.

Looking around for solutions I've think the problem lies in the server settings (Nginx), but as this is even newer for me I would like to verify this before I mess it up.

The CSS is placed within django_project/static/css, this folder is not accessible using a browser:

IPadress/static/css/style.css serves a '404 Not Found' error on the CSS

Edit

I just found the Nginx error log which states looking in for the static files in django_project/django_project/static/css which is nonexistent. I presume this should be corrected in settings.py. Am I correct?

Settings.py static:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Sumerechny
  • 148
  • 10

2 Answers2

0

Is this when you're attempting to deploy your website to production for the first time?

If you're running the site locally with 'python manage.py runserver' then your setup looks ok as long as your file paths match, however if you are trying to serve static files in production you will need to serve them differently, I personally use whitenoise to do this from the webserver itself.

Documentation: https://djangobook.com/serving-files-production/

0

The webserver will look at /static/ location and you need to provide mapping of virtual path /static/ to physical path and grant permissions to access from everyone.

The below settings are required to put in your webserver configuration file. Replace "/opt/tutor/static/" to value of os.path.join(BASE_DIR, 'static').

Alias /static /opt/tutor/static/
Alias /media /opt/tutor/media/
<Directory /opt/tutor/static/>
    Require all granted
</Directory>
<Directory /opt/tutor/media/>
    Require all granted
</Directory>

Put above things inside block. Remove media setting in case you don't have requirement of IPAddress/media/ urls.

Elixir Techne
  • 1,848
  • 15
  • 20