Skip to main content How to load static files while deploying using nginx : r/django
r/django icon
Go to django
[deleted]

How to load static files while deploying using nginx

Hosting and deployment

Everything is working fine except for static files I only have STATIC_URL and staticfiles app is in my INSTALLED_APPS in my settings file and when I request the admin page i get response without styling What should I do to load the static file

Archived post. New comments cannot be posted and votes cannot be cast.
Launch Your Django App into Orbit
Thumbnail image: Launch Your Django App into Orbit
Sort by:
Best
Open comment sort options
  1. When deployed you need to run manage.py collectstatic, this is what moves your files to you static_dir

Then in nginx config, you need to set access to the static url:

I did:

location /static/ {

                alias /my/local/path

}

Good luck

Same for media files

[deleted]

It finally worked after I traced the error it was because of misspelling in the path Now i have couple of things to understand In deploying why do i need static_root and staticfiles_dirs when it works with just static_url and collectstatic

More replies
[deleted]

I did these earlier

STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] STATIC_ROOT = os.path.join(BASE_DIR, 'static') I restarted the service and it didn’t work

More replies
More replies

You can use whitenoise. https://github.com/evansd/whitenoise

Go into your Linux and navigate to one of your actual static files. Then type pwd, which will print the current path

So if your static file is 'logo.jpg' and the path is /home/deepholedeveloper/project/static/images/logo.jpg

Then you want nginx to point to /home/deepholedeveloper/project/static/

Basically, Nginx is serving your static so it doesn't really matter where Django points to. All that matters is where you've put your collectstatic files (and if you haven't run collectstatic yet, do that)

Korbit’s AI-Powered Code Review Agent helps your team ship better code, faster.
Thumbnail image: Korbit’s AI-Powered Code Review Agent helps your team ship better code, faster.
[deleted]

Comment deleted by user

[deleted]

Oh jeez. This comment is a mess. There's a lot wrong with it. Please delete it

its common problem in nginx but the solution was to use location

location /static/ {
  alias path
}

make sure your path was an / in the end. /app/static/