1

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"

4
  • 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? Commented Feb 18, 2019 at 21:20
  • yes, the html that is generated contains links like: <img src="/static/admin/img/search.svg" alt="Search"> Commented Feb 18, 2019 at 22:06
  • In you nginx conf file, add a / for static files as: location /static/ {alias /home/pi/DigitalClock/dcvenv/static/ ;}, also try removing autoindex on. Commented Feb 19, 2019 at 12:16
  • Thank you. I tried that. No luck. Commented Feb 19, 2019 at 14:33

1 Answer 1

0

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/')
Sign up to request clarification or add additional context in comments.

9 Comments

I assume you meant "PROJECT_ROOT". Tried what you suggested; changed setttings.py with those entries, reran python manage.py collectstatic, restarted nginx/gunicorn, refreshed the browser. Same problem. Only difference is now i can brows to 192.168.1.02/staticfiles also.
project urls.py now looks like; urlpatterns = [ path('admin/', admin.site.urls), path("", include('dchome.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) I restarted nginx/gunicorn and pointed browser at 192.168.1.20/admin. got NameError at /admin/ name 'static is not defined
and yes, debug=true in project settings.py
Sorry forgot your suggested imports to urls.py. point browser at 192.168.1.20/admin and now get: "ImproperlyConfigured at /admin/ Empty static prefix not permitted.
I tried your latest suggestion. now the app works, but no static content.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.