0

I may be making a simple mistake but after several days of troubleshooting and iterating I cannot locate the source of my problem. Despite my best efforts Django refuses to load my static css file but does load html and spits out exactly the same error each time. The error is Not Found: /"/static/index.css" Here is my current code:

APP_NAME/APP_NAME/templates/index.html

    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href=“{% static 'index.css' %}”>

APP_NAME/urls.py

    from django.conf.urls import url
    from django.contrib import admin
    from django.conf import settings
    from django.conf.urls.static import static

    urlpatterns = [
        url(r'^APP_NAME/', include("APP_NAME.urls")),
        url(r'^admin/', admin.site.urls),
    ] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)

APP_NAME/APP_NAME/urls.py

    from django.conf.urls import url
    from django.conf import settings
    from django.conf.urls.static import static

    from . import views

    urlpatterns = [
            url(r"^$", views.index, name = "index"),
    ] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)

APP_NAME/APP_NAME/settings.py

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


    # SECURITY WARNING: don't run with debug turned on in production!

    DEBUG = True

    ALLOWED_HOSTS = []


    # Application definition

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]



    ROOT_URLCONF = 'APP_NAME.urls'

    STATIC_URL = "/static/"
    STATIC_ROOT = os.path.join(BASE_DIR, "APP_NAME/static/")

    STATICFILES_DIR = [
            os.path.join(BASE_DIR, "APP_NAME/static/")
    ]
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
torchhound
  • 510
  • 6
  • 15

2 Answers2

1

You don't need to add static URLs - django searches and serves static files for you if DEBUG=True. Try

findstatic index --verbosity=2

to view where static files are actually searched. as suggested here Django STATIC_URL only works without leading slash

Community
  • 1
  • 1
Eugene Prikazchikov
  • 1,794
  • 1
  • 13
  • 11
  • This was very helpful, `findstatic` showed the only search location as `/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/static`. Django is not searching in my APP_NAME/APP_NAME/static folder. How do I make Django search there? – torchhound Oct 31 '16 at 17:39
  • By default it looks in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting. This can configured via STATICFILES_FINDERS and STATICFILES_DIRS settings - https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATICFILES_FINDERS So, you can put APP_NAME/APP_NAME/static to STATICFILES_DIRS setting. Or probably it would be easier to put static files to static/ dir of one of your apps. – Eugene Prikazchikov Oct 31 '16 at 18:06
  • I realized I used `STATICFILES_DIR` instead of `STATICFILES_DIRS` but even though I corrected this error and added `STATICFILES_FINDERS` manage.py runserver still returns `Not Found: /"/static/index.css"` – torchhound Oct 31 '16 at 19:13
  • Additionally once I fixed the typo `findstatic` now finds `index.css` but `manage.py runserver` still returns `Not Found` – torchhound Oct 31 '16 at 23:53
1

You have wrong type of quotes

<link rel="stylesheet" type="text/css" href=“{% static 'index.css' %}”>
                                            ^                        ^

Use normal ones

<link rel="stylesheet" type="text/css" href="{% static 'index.css' %}">
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63