0
votes

I am trying to run the django admin app using gunicorn 19.19.0 and nginx 1.10.3 on a raspberry pi 3 (raspian 9) with python 3.5.3 and django 2.1.7. Nginx seems to be working properly and the nginx and gunicorn error logs are empty. The app will not display any static content however.

I checked the nginx.conf file.

I ran collectstatic and checked that all the files are there.

I can point a browser to 192.168.1.20/static and it shows the right directory.

I can also browse to all the files.

I tried following the path in the nginx.conf file with a '/'

All functions of the admin app work fine. just no static content.

I've googled and read/tried every forum fix that i can find.

I have also run the python development server (python manage.py runserver). In that config static content shows just fine.

nginx.conf file

events{}

http {
    server {
            listen       80;
            server_name  localhost;

            location /static {
                    autoindex on; 
                    alias /home/pi/DigitalClock/dcvenv/static;
            }
            location / {
                    error_log /home/pi/DigitalClock/dcvenv/nginx_err.log;
                    access_log /home/pi/DigitalClock/dcvenv/nginx_acc.log;
                    proxy_pass http://127.0.0.1:8000;
            }
    }
}

gunicorn start command

gunicorn dcweb.wsgi:application --bind localhost:8000

django project settings file

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

last entries of ngnix_acc.log (*_err.log is empty)

192.168.1.10 - - [18/Feb/2019:12:45:43 -0800] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0 "http://192.168.1.20/admin/login/?next=/admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 192.168.1.10 - - [18/Feb/2019:12:45:43 -0800] "GET /admin/ HTTP/1.1" 200 4944 "http://192.168.1.20/admin/login/?next=/admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 192.168.1.10 - - [18/Feb/2019:12:45:59 -0800] "GET /admin/auth/group/ HTTP/1.1" 200 3500 "http://192.168.1.20/admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 192.168.1.10 - - [18/Feb/2019:12:45:59 -0800] "GET /admin/jsi18n/ HTTP/1.1" 200 3185 "http://192.168.1.20/admin/auth/group/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"

1
Your access log is defined for the location / only. Its not surprising there is no static entries in this log. Is the html generate contain static links?danblack
yes, the html that is generated contains links like: <img src="/static/admin/img/search.svg" alt="Search">Sam Melehy
In you nginx conf file, add a / for static files as: location /static/ {alias /home/pi/DigitalClock/dcvenv/static/ ;}, also try removing autoindex on.Reema Parakh
Thank you. I tried that. No luck.Sam Melehy

1 Answers

0
votes

place this code in your settings.py, then you have the collectstatic, also test if the DEBUG = True

ROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # specify static root

add in your url project

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
   # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

update Try this way for your project.:

urlpatterns = patterns('',
....urls......
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

in your settings.py

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

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

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

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(REPOSITORY_ROOT, 'media/')