Django : Static content not found

vivekanon picture vivekanon · Jan 3, 2015 · Viewed 16.8k times · Source

I have been breaking my head over this for a full day but can't figure out the problem. It happened after I copied my project from one machine to another.

Settings.py

STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

Mentioned 'django.contrib.staticfiles' in INSTALLED_APPS as well.

Folder structure :

Django-Projects (root)
    project
    app
    static
        css
          home.css
        js
    manage.py

Template :

{% load staticfiles %}

<link rel="stylesheet" href="{% static 'css/home.css' %}">

urls.py

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('app.urls')),
)

It throws an error in the console on opening the template :

 GET http://127.0.0.1:8000/static/css/home.css 
Failed to load resource: the server responded with a status of 404 (NOT FOUND)

What might be wrong here ? Please help me out. Much thanks!

Answer

tilaprimera picture tilaprimera · Jan 3, 2015

In your settings.py

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

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

Then in your template file:

<link rel="stylesheet" href="{{ STATIC_URL }}css/home.css">

With the directory root structure that you have shown, I think the above setting should work. Have not tested it though. Let me know if it works.