0

My static files aren't being served correctly because there's a strange space in my folder structure.

development folder structure

static
 >css
  >lots of css files
 >js
 >img

When I run collecstatic to serve them properly, collect static puts everything into a folder called staticfiles, here's my settings.py

settings.py

STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [
    STATIC_DIR
]

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

# MEDIA file defaults
MEDIA_ROOT = os.path.join(BASE_DIR,'staticfiles/media')
MEDIA_URL = '/staticfiles/media/'

I use it this way as this is how its configured on my live server. I now have (in my development env) a folder in the project called staticfiles/static, inside which I can find the a similar folder structure as above.

staticfiles
 >img
 >media
 >static
   >css
   >admin
   >img
   >podcast
   >js

However, once I've run the server, django thinks my CSS file 'static/%20css/front.css' and so it doesn't load. If I correct the URL to 'static/css/front.css' the css file loads perfectly...?

Here's where it's trying to get the css from: http://127.0.0.1:8000/static/%20css/front.css

Here's where it actually is: http://127.0.0.1:8000/static/css/front.css

I've checked the folder name for 'css' on both my development static, and the staticfiles static, and it is correct. It seems to be something to do with the way it's written the folder path?

phil0s0pher
  • 525
  • 10
  • 21
  • How are you loading these static files? With `{% static %}` template tag? – Borut May 28 '19 at 11:48
  • Looks odd. Try commenting out `STATIC_DIR` and `STATICFILES_DIR`, they are redundant. – Nikita Tonkoskur May 28 '19 at 11:49
  • Hmmm... I'm loading with {% load staticfiles %} – phil0s0pher May 28 '19 at 11:52
  • When I comment out STATIC_DIR and STATICFILES_DIR my logo stops loading, but I can't see any other reference to those variables in settings.py. This could be the source of the problem, something else is calling that variable in my settings.py and making use of it, and possibly this could be the issue. – phil0s0pher May 28 '19 at 11:55
  • A search through the whole project produces no other reference to STATICFILES_DIR or STATIC_DIR, and yet if I comment it out, my logo disappears. – phil0s0pher May 28 '19 at 11:57
  • I've found the problem - this is a bug (i think) with Atom with page reloading/copy pasting. Occasionally, I get an inexpicable ' ' whenever pasting in {% url static 'css/front' %} It's not actually the first time this has happened, but I assumed it was Django not Atom. – phil0s0pher May 28 '19 at 11:59
  • 2
    Not related, but STATIC_DIR should *not* be a member of STATICFILES_DIRS. – Daniel Roseman May 28 '19 at 12:08

0 Answers0