简体   繁体   中英

Django - 404 static files not found

I know theres about a millon of these questions but non of them have helped me out.

I can't access my static files for deployment, I've slpit my settings file into base and production and put them in a folder in the settings.py orginal file.

I've done everything neccessary but it still doesn't seem to be working and I can't work it out for the life of me.

I've tried editing the path several time and no change.

Maybe I've missed something obvious and someone else can see it.

  1. venv
  2. --project
  3. ----app1
  4. ------static folder
  5. ----wsgi folder
  6. ------settings_old.py
  7. ------new settings folder
  8. ---------base.py
  9. ---------production.py

base.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = ['django.contrib.staticfiles',]

STATIC_URL = '/static/'

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

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

I've tried a couple of different things like changing from BASE_DIR to a new PROD_DIR that directly goes to the static file in index.

PROD_ROOT = os.path.dirname(os.path.abspath('__file__' ))
PROD_ROOT= os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath('__file__' ))))
PROD_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

Nothing seems to be working.

UPDATE:

as suggested by other I've tried collectstatic, the folder env/project/static_r was found however, i get

'0 files where copied to static_r'.

furthermore, also as suggested I printed out base_dir and prod_dir and got the following

"prod is /user/venv/project" & "base is /user/venv/project/wsgi-folder"

ps this is after I edited prod_dir to -

PROD_ROOT = os.path.dirname(os.path.abspath('__file__' ))

I am not really sure, but are you sure it is STATIC_DIRS and not STATICFILES_DIRS? '-'

Based on your settings, your static files should be located in /project/static/ instead of in the individual app.

You will also need to load the static files as stated in the documentation

{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>

What you can always do to test your paths is to print it.

so in your settings do

print BASE_DIR, STATIC_DIRS, STATIC_ROOT

and you'll see where your error is.

In my opinion it's the fact you're putting the static folder within the app, rather than within the project, because collectstatic will put all static files in one directory regardless

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM