0

I have a project-level directory named static which contains various .css, images, and javascript files. My settings file contains:

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

INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
]

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = '/static/' # I've tried with and w/o this
STATICFILE_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

My urls.py file contains: from django.conf import settings from django.conf.urls.static import static

urlpatterns = [
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

I've printed out BASE_DIR and it points to the correct directory. Yet, when I load my webpage, I receive this error:

Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/css/style.css

This file exists, and it exists in exactly the location that Django is trying to access. Still, these files aren't found. I'm a bit at my wits end—the documentation has no information outside of what I've already done, and I've searched around for hours. All of the already-answered questions here regard STATICFILE_DIRS. This question, for example, points out that—when DEBUG = True—one doesn't have to run collectstatic and using STATICFILE_DIRS should suffice.

Edit: GOD DAMNIT. It's STATICFILES_DIRS and not STATICFILE_DIRS. That was my problem. -_-

AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
  • Have you added the required settings to your urls? (https://docs.djangoproject.com/en/2.1/howto/static-files/#serving-static-files-during-development) – art Dec 18 '18 at 00:21
  • @art06 I have, yes (I edited that into my question), but I believe that this is only necessary if one doesn't have `django.contrib.staticfiles` in one's `INSTALLED_APPS` – AmagicalFishy Dec 18 '18 at 00:57

1 Answers1

0

In your urlpatterns, you have,

urlpatterns = [
  ...
]  + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

This means the static url must be the path, and the static root be the directory, but in your settings.py your static root is set to '/static/', which is wrong.

Here is what you can do,

  1. Remove both STATIC_ROOT, STATICFILE_DIRS and also from the urlpatterns + static(...). And add load static in template , which will work correctly.

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

    then add + static(...) part in urlpatterns.

Although you don't need to do the 2nd one. The first one will work fine.

Bidhan Majhi
  • 1,320
  • 1
  • 12
  • 25