I have a django webapp deployed on ubuntu server using gunicorn and nginx. The website’s functionality is working fine, however some (not all) CSS is not working on the production environment which was working fine locally. Not a single image is displayed as well.
I did run python3 manange.py collectstatic
Here is my static url in my settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
# Static root for production
STATIC_ROOT = BASE_DIR / 'staticfiles'
# Additional directories where Django looks for static files
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
In my nginx configuration I have
location /static/ {
root /home/vishesh/spidernet;
}
Folder structure for my static directory in the project:
projectdir/static/images,css
I tried so much to fix this, please help!
I think the root here:
should be to your static root. I don’t know exactly what the path is, but I think it should be ending in staticfiles
in your case, given this:
1 Like
First, managing static files in a production environment is completely different than managing them in development.
You want to forget everything you’ve done in development - it just doesn’t apply, and relying upon that information is not useful. You also don’t want to make references to that information here, it doesn’t help.
Briefly:
- Do not server static files from within your project. Pick an “nginx-friendly” location as the desired target of those files.
- Ensuring permissions are correct is critical.
- Your
STATIC_ROOT
needs to correspond to the path you specify in either your alias
or root
directive in nginx.
- Your
STATIC_URL
needs to correspond to the location
directive.
The docs at How to deploy static files | Django documentation | Django provide a summary. Also, this is a topic that has been talked about a lot here. For some examples, see:
Also, if you do a search through the deployment section for nginx, you’ll find a number of other working examples.
1 Like
I tried making those changes. However, the css and images are still not getting displayed.
in my settins.py
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATIC_URL = '/static/'
in my nginx conf
location /static/ {
root/home/vishesh/spidernet/staticfiles;
}
There should be a space after root
.
If that still doesn’t work, what do you get if you do this on the command line:
ls /home/vishesh/spidernet/staticfiles
?
/spidernet$ ls /home/vishesh/spidernet/staticfiles
admin css images