3

Having problem with staticfiles in Django 2.

        BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
        STATIC_URL = '/static/'
        STATIC_ROOT = BASE_DIR + '/static/'
        STATICFILES_DIR = [
              (BASE_DIR + "static/"),
        ]


        STATICFILES_FINDERS = [
              'django.contrib.staticfiles.finders.FileSystemFinder',
             'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        ]

And my static structure is like below

enter image description here

I am getting 404 for statics. Tried different things but no success. btw collectstatic has been already performed. Another thing is when i make changes in the static folder, collectstatic is not collecting anything. it says "unmodified".

i am newbie in Django. appreciate for your help.


UPDATE 2:

New structure is below enter image description here

and the settings is like

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static' )
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_files')
]

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

When i try to collectstatic, there is no change. seems it is not collecting. and 404 returns for statics naturally since it is not collecting.

j-i-l
  • 10,281
  • 3
  • 53
  • 70
CorpusCallosum
  • 179
  • 1
  • 1
  • 10

2 Answers2

4

There are 2 issues:

1. is the syntax error that Digitiain pointed out in his answer:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

2. You cannot add the path in STATIC_ROOT to your STTATICFILES_DIRS. So if your STTATICFILES_DIRS looks like above, STATIC_ROOT cannot be os.path.join(BASE_DIR, "static").


The first point should be fairly clear. The second also confused me in the beginning. But it makes a lot of sense actually:

In STATICFILES_DIRS you tell django where to go and look for static files. This not primarily to serve them, but to collect them! The point is that on a productive server, django is not supposed to serve static files; this task should be handled by the web server. Check the docs for details. The idea is that you run the command python manage.py collectstatic and django will go through all the paths in STATICFILES_DIRS, collect the static files and put them... - exactly! - in the STATIC_ROOT folder. Now it becomes clear why the STATIC_ROOT folder cannot really be part of STATICFILES_DIRS as this would mean django would need to put the folder into itself. Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here is more from the docs on that.

I hope that helps to clarify the relation between STATICFILES_DIRS and STATIC_ROOT. Bottom-line: Choose a different path for STATIC_ROOT and you should be good to go!


As an example, you could have a project structure like this:

base_dir/
  - static/
      - css/
      - js/
      - ...
  - static_collected/
  - ...

Then it would be valid to define:

# settings.py
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_collected")
culix
  • 10,188
  • 6
  • 36
  • 52
j-i-l
  • 10,281
  • 3
  • 53
  • 70
  • yea really confusing at first. now my config is STATIC_URL = '/static/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static" ) STATICFILES_DIRS = (os.path.join(os.path.dirname(BASE_DIR)), "static") and collectstatic is working. but i guess having problem with path since it is creating new static folder out of project folder if it is not normal behavior for django – CorpusCallosum Jan 14 '18 at 20:14
  • @CorpusCallosum please note that if `STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static" )` and `STATICFILES_DIRS = (os.path.join(os.path.dirname(BASE_DIR)), "static")` then **STATIC_ROOT is inside STATICFILES_DIRS**! If `BASE_DIR` is in `STATICFILES_DIRS` then any dir withing `BASE_DIR` cannot become the `STATIC_ROOT`. Please just choose some random folder **outside** `BASE_DIR` for your `STATIC_ROOT` and try if things work better. – j-i-l Jan 14 '18 at 20:32
  • @CorpusCallosum added an example. I hope this helps to get your setup running. Let me know if it should still not work. – j-i-l Jan 14 '18 at 22:18
  • really appreciate your effort. i added an update to the original question. – CorpusCallosum Jan 14 '18 at 22:50
  • @CorpusCallosum sorry to hear that it still does not work. do you have `'django.contrib.staticfiles',` under INSTALLED_APPS? – j-i-l Jan 14 '18 at 23:03
  • Yep thats the first thing i have controlled. This becomes really pain in the ass. Really weird. – CorpusCallosum Jan 14 '18 at 23:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163158/discussion-between-jojo-and-corpuscallosum). – j-i-l Jan 14 '18 at 23:17
1

This might be a red herring, but in the Django docs the syntax for setting a path relative to your BASE_DIR includes os.path.join, so yours would look like:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

EDIT: I also just noticed that you referenced DIR rather than DIRS, which could be causing grief. Ditto with templates.

Digitiain
  • 23
  • 5
  • when i use join, it says ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. thats why changed to string conct. – CorpusCallosum Jan 14 '18 at 18:14