skip to Main Content

I’m trying to deply my django site, and I did to my ip address, but when I load page online it doesn’t load static files. I tried changing different settings on a server and on django files, but I’m out of ideas. I would appreciate a pair of fresh eyes.

Please note that I followed this tutorial (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu) and everything went well as explained in tutorial, apart from loading static files. When I tested port :8000, static was loaded.

for sites-available I did this:

sudo nano /etc/nginx/sites-available/django-site

Output:

server {
    listen 80;
    server_name xx.xx.xxx.xx; #my ip address

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /home/muser/django-site/staticfiles/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

this is settings.py

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

STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
]

#media
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

It might be that this is all correct, but it might be that urls aren’t set up properly. So please see urls.py

urlpatterns = i18n_patterns(
    path(_('admin/'), admin.site.urls),
    path('rosetta/', include('rosetta.urls')),
    path('', include('my_app.urls', namespace='my_app')),
)
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

Any ideas what I did wrong? Thanks in advance!

2

Answers


  1. you use static in setting, but you add staticfiles in nginx conf.

    server {
        ...
        location /static/ {
            # alias /home/muser/django-site/staticfiles/;
            alias /home/muser/django-site/static/;
        }
        ...
    }
    
    Login or Signup to reply.
  2. My suggestion is to change the name of the folder where ALL THE STATIC FILES WILL BE COLLECTED using the collectstatic command to static and the base static folder that all apps use to staticfiles or any other name you prefer like this:

    STATIC_URL = 'static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
    STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'staticfiles'),
    ]
    

    and then this should work and all your static files will be served, of course leave all configurations of nginx and gunicorn as is.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search