Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Static content not found

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!

like image 915
vivekanon Avatar asked Jan 03 '15 13:01

vivekanon


2 Answers

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.

like image 126
hlkstuv_23900 Avatar answered Oct 19 '22 18:10

hlkstuv_23900


set DEBUG=True and see if it works .. this means, django will serve your static files, and not httpserver which in this case doesnt exist since you run the app locally.

like image 34
doniyor Avatar answered Oct 19 '22 19:10

doniyor