Assets are not loading

Hi

I have deployed my site to Elastic Beanstalk. And it seems My assets are not loading. If I do some work arounds found from stackoverflow I get into a weird situation. If admin assets load, then site assets are gone and vice-versa.

I am using Django 4.0.1, with Nginx server on Amazon Linux 2.

settings.py:

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

I tried to reload page many times, disabled browser cache from dev tools in chrome.
Also I get 404 for assets on site using above config in settings.py but admin assets are working.

Any help would be great.

Thanks

Are you doing your “collectstatic” after deplying the code? Does the web server have the appropriate permissions to that directory? What does your nginx configuration look like for your /static/ path?

I have a post-deploy platform hook which runs, collectstatic:

if [[ "$EB_IS_COMMAND_LEADER" == "true" ]]; then
  python manage.py migrate --noinput
  python manage.py createsu
  python manage.py collectstatic --noinput --clear
fi

I confirm from logs from EC2 instance, its executing without error.
I haven’t created any nginx config for beanstalk. Do I need one? I am assuming it works out of the box.

Thanks

I wouldn’t know. I don’t know what it’s configuration is or how it’s running your application.

But by running collectstatic, you’re replicating your static files into BASE_DIR/static/. Since you’re doing this in order to allow nginx to serve those files directly (rather than having Django do it), I’m assuming some configuration would be required.

I have gone through like 200 stackoverflow threads and atleast 200+ google searched websites. I have same results and no one mentioned to change nginx. So I am assuming Something is wrong on django side.

There are other workarounds - but that’s just what they are, workarounds.
In a production environment, Django is not intended to serve static files. See Deployment checklist | Django documentation | Django and How to deploy static files | Django documentation | Django

I just tested on my local with same configuration I have same error. Admin css is working but main site css, js not working.

What is your local configuration?
Are you using nginx? (If not, what webserver are you using?)
Are you running your Django app within uwsgi? (If not, what app server are you using?)
What are the specific directory structures involved? (Both for your app and your destination for your static files.)
Have you read and configured your environment according to the deployment docs referenced previously?

Also, do they work at all under any combination of configurations? (Most importantly, is it working if you’re just running this using runserver?) There is a possibility that you don’t have your staticfiles app configured correctly or that you’re not referring to the right urls for those files.

HI

On my local it works with this config in settings.py:

STATIC_URL = 'static/'
STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)

And I am using this command on my local:
python manage.py runserver

With same configuration on AWS Beanstalk AL2, i get below error:

You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

This environment is configured to work with nginx automatically.