0

When I tried to issue commands to collect staticfiles

In [41]: ! python manage.py collectstatic

It throw FileNotFoundError:

FileNotFoundError: [Errno 2] No such file or directory: 
'/Users/me/Desktop/Django/forum/static'

Nevertheless, there exist file '/Users/me/Desktop/Django/forum/static'

In [44]: ! tree -L 2
.
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── forums
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── ll_forum
│   ├── bin
│   ├── include
│   ├── lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
└── manage.py

14 directories, 15 files

The setting.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #my apps
    "forums"
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), #notice the comma
)
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • https://stackoverflow.com/a/34586172/5442966 – Junior Gantin May 23 '18 at 12:38
  • @JuniorGantin It's not the good answer according to his issue. – Essex May 23 '18 at 12:48
  • Note that you don't have to set `STATIC_ROOT` or run `collectstatic` until you deploy your project into production. Since you have `'django.contrib.staticfiles'` in your `INSTALLED_APPS`, `runserver` will take care of serving static files for you. – Alasdair May 23 '18 at 13:00

2 Answers2

1

You should provide the STATIC_ROOT in your settings, by default Django looks for a directory named static in your root project, so that 's why your get this /Users/me/Desktop/Django/forum/static'``FileNotFoundError the folder is not present.

You have that directory static next to your settings, it should not be there. By the way also your templates should not be there.

Move them back to your root project

├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
|   |---static # remove this
│   ├── templates # remove this here
│   ├── urls.py
│   └── wsgi.py
|---static -- # GOOD
|---templates # GOOD
Lemayzeur
  • 8,297
  • 3
  • 23
  • 50
1

You need to have static directory outside from Django App. This directory and your Django app should be separate.

Then, if you specify :

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), #notice the comma
)

It means your static directory is in your base directory. Or you have only forum, forums, ll_forum ...

If you want absolutly keep your static directory in your Django App, make this directory in the BASE_DIR (move from initial position to your base project) and write something like this in settings.py file :

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

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')

Finally, you should have something like this :

.
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── forums
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── ll_forum
│   ├── bin
│   ├── include
│   ├── lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
└── manage.py
│
└── static
│
└── templates
Essex
  • 6,042
  • 11
  • 67
  • 139