3

A while ago, I attempted to use "collectstatic" in Django to serve my static files and failed miserably. Now I am finally trying to get my static files serving correctly in my development environment. I can't figure out what has gone wrong.

When I run python manage.py findstatic images/add.png

console returns:

base path: C:\Projects\AlmondKing\AlmondKing
static files dirs: C:\Projects\AlmondKing\AlmondKing\static
No matching file found for 'images/add.png'.

The directory paths are correct, but it still can't locate my files. I've tried a number of different settings configurations to no avail. Can anyone spot my problem? Here are what I think are all the relevant settings:

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

DEBUG = True

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'AlmondKing.InventoryLogs',
    'AlmondKing.FinancialLogs',
    'AlmondKing.AKGenius',
)

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

print("base path:", BASE_DIR)
print("static files dirs:", STATIC_FILES_DIRS)

EDIT: I found the findstatic --verbosity 2 command and the only directory being searched is: C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\contrib\admin\static

Apparently it is only looking in my virtual environment, instead of my project directory. Is this normal Django behavior?

Adam Starrh
  • 6,428
  • 8
  • 50
  • 89

2 Answers2

8

The setting is STATICFILES_DIRS, not STATIC_FILES_DIRS.

You are also missing a comma in the setting. Without the comma it is treated as a string not a tuple.

It should be:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Good looking out thank you. Unfortunately, it still tells me that no matching file was found. – Adam Starrh Sep 05 '15 at 20:38
  • What is the location of the file `images/add.png` that you are trying to find? – Alasdair Sep 06 '15 at 12:19
  • The full filepath is C:\Projects\AlmondKing\AlmondKing\static\images\add.jpg - I just navigated to it again to confirm – Adam Starrh Sep 07 '15 at 18:20
  • You've written `.png` in the question and `.jpg` in the comment. Is that what the issue is? Apart from that, I can't spot any problems. Hope you figure it out. – Alasdair Sep 07 '15 at 18:27
  • No, I just wrote it wrong in the comment. The file is indeed actually a .png – Adam Starrh Sep 07 '15 at 19:00
  • I just noticed this change introduced a weird result. Static directory is now printing as: `('C:\\Projects\\AlmondKing\\AlmondKing\\static',)` I don't think that is what I want. – Adam Starrh Sep 08 '15 at 20:27
  • 1
    That's ok, it's meant to be a tuple, and the double backslash shows they are actual backslashes, not escaping the next character. Django actually recommends using forward slashes for paths, even for Windows. – Alasdair Sep 08 '15 at 23:26
  • Ah! Thanks for explaining that! – Adam Starrh Sep 09 '15 at 02:28
  • I've spotted the problem, you've misspelled the setting. It should be `STATICFILES_DIRS`. – Alasdair Sep 09 '15 at 09:06
  • Wow. How did I miss that? Thank you for your attention. It is working now. – Adam Starrh Sep 09 '15 at 20:47
2

At last! Referring here, I learned that the static files directory must be located within the directory of an individual app. I had the static directory in the project root. I moved it into an app directory and it is working now.

This whole project will be using these same static files. If someone knows, do I need to have a copy of them in every App's directory, or can all of my project's apps share a static directory? Thanks!

Community
  • 1
  • 1
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
  • 1
    I'm glad you got it working, but it's incorrect to say the static files must be within the directory of an individual app. From [the docs](https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/#collectstatic): "The default is to look in all locations defined in `STATICFILES_DIRS` and in the 'static' directory of apps specified by the `INSTALLED_APPS` setting." If you're happy with the files in one app's static directory, then leave it like that -- you don't need to copy the files into every app's static directory. – Alasdair Sep 08 '15 at 23:33
  • Okay, well for some reason it was only scanning a single location in my virtual environment until I moved the static directory (see my edit for the result of `findstatic --v 2`). I could not get `findstatic` to scan the directory I fixed as `STATIC_FILES_DIRS`, even when I hardcoded the path in directly. Any idea why this might be? Could it have something to do with `virtualenv` or `virtualenvwrapper`? – Adam Starrh Sep 09 '15 at 02:32
  • 1
    I've spotted the problem, you've misspelled the setting. It should be `STATICFILES_DIRS`. – Alasdair Sep 09 '15 at 09:04