0

Deployed (on server) Django 3 project doesn't sees static and css files(whole static folder), the local on development machine works perfectly.

The guide that I have followed is this.

  • I have et up a Digital ocean droplet with ubuntu 18.04
  • my static files can be found here
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mymainapp/static/')]
#STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Removed based [on](https://stackoverflow.com/questions/60354519/django-base-html-extended-to-homepage-html-static-images-appear-but-home-css-d)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mymainapp/media')
MEDIA_URL = '/media/'
  • Than all the other staff with Postgres, Nginx, and Gunicorn as in the guide
  • but the static files (image, SVG, css, files doesn't load in)
  • I have my bootstrap CSS files downloaded to local directory
  • The website is online and if I press F12 in chrome it puts out the following error messages (turning DEBUG = True does not deliver any more error messages or error page)
Failed to load resource: the server responded with a status of 404 (Not Found)
homepage03_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
logo_03c_small.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
jquery.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
popper.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
tiktok_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
instagram_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
twitch_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
twitter_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
youtube_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
facebook_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
mobile_features_large_compressed.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
creator_platforms_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
creator_sm_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
reoccuring_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
stripe_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
customerchat.php?app_id=&attribution=setup_tool&channel=https%3A%2F%2Fstaticxx.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D46%23cb%3Df2ca0722231e688%26domain%3D159.65.234.146%26origin%3Dhttp%3A%2F%2F159.65.234.146%2Ff167133a445f9a4%26relation%3Dparent.parent&container_width=0&locale=en_US&page_id=2172253486430236&sdk=joey:1 Failed to load resource: the server responded with a status of 500 ()
paypal_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
googleads1_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
googlepay_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
amazon_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bitcoin_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
logo_02_Background.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
shirts_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
message_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
sopnsor2_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
socialmedia_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
combinedbarnad_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
applepay_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
check.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
feature_description_laptop_cut.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
self_image_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
reddit_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
linkedin_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
collie_2_comp.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
DevTools failed to parse SourceMap: chrome-extension://hnmpcagpplmpfojmgmnngilcnanddlhb/browser-polyfill.min.js.map
DevTools failed to parse SourceMap: chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.preload.js.map
DevTools failed to parse SourceMap: chrome-extension://hnmpcagpplmpfojmgmnngilcnanddlhb/browser-polyfill.min.js.map
DevTools failed to parse SourceMap: chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.postload.js.map
  • I had the same errors before going thru the guide and just haveing a basic pulled git version of it and running with python3 manage.py runserver it loads the site but not the css and the static elements
  • I have put the base /media and /static folders in to git ignore (these should be the one where collect static collets up the element). The original static is under the myowndjango-project/myowndjango/static and myowndjango-project/myowndjango/media
  • I have also set up the nginx as follows
...
location /static/ {
        root /home/myserveruser/myowndjango-project;
    }
    location /media/ {
        root /home/myserveruser/myowndjango-project;
    }
...

What I have tried to solve it

  • look for server access log
    • run /var/log/nginx/access.log result: -bash: /var/log/nginx/access.log: Permission denied
    • run sudo /var/log/nginx/access.log result: sudo: /var/log/nginx/access.log: command not found
  • changing nginx file
...
location /static/ {
        root /home/myserveruser/myowndjango-project/myowndjango/static;
    }
    location /media/ {
        root /home/myserveruser/myowndjango-project/myowndjango/media;
    }
...
  • Running python3 manage.py collectstatic (restarted nginx and gunicorn) and it still does the same error.
sogu
  • 2,738
  • 5
  • 31
  • 90
  • 1
    Look in the http server access log to see the actual requested URLs of the failing resources. Are the URLs correct? – John Gordon Mar 25 '20 at 19:17
  • How can I do that? – sogu Mar 25 '20 at 19:18
  • 1
    Your http server must be logging requests somewhere. Find that log file and look at it. – John Gordon Mar 25 '20 at 19:19
  • run ```/var/log/nginx/access.log``` result: ```-bash: /var/log/nginx/access.log: Permission denied``` run ```sudo /var/log/nginx/access.log``` result: ```sudo: /var/log/nginx/access.log: command not found``` – sogu Mar 25 '20 at 19:20

2 Answers2

1

You have wrong folder as location in nginx for static files you should put the full path to exact static file/ media file folder

location /static/ {
        root /home/myserveruser/myowndjango-project/myowndjango/static;
}
iklinac
  • 14,944
  • 4
  • 28
  • 30
  • I am a little bit mixed up on this. When I do ```collect static``` on the project it takes ```myowndjango/static``` adn boundel it in to ```myowndjango-project/static``` that is in git ignore as /static. Than why I am not using that and why is tat the ignored folder? – sogu Mar 25 '20 at 21:03
  • I have made the modifications as you said. restarted nginx and gunicorn ```sudo nginx -t && sudo systemctl restart nginx``` and ```sudo systemctl restart gunicorn``` but I still ahev the errors – sogu Mar 25 '20 at 21:29
0

There is 3 useful resource that answers this question from the official Django 3 documentation

create a local_settings.py file that should replace/add properties to your normal setting files in deployed mode.

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

Depending on your situation imports

sogu
  • 2,738
  • 5
  • 31
  • 90