vivekanon
vivekanon

Reputation: 1823

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!

Upvotes: 12

Views: 17941

Answers (6)

Lily H.
Lily H.

Reputation: 194

I had the same issue in Django. I added to my settings: STATIC_ROOT = 'static/'

It was the only problem.

Upvotes: 0

Wu Jakey
Wu Jakey

Reputation: 31

I had researched this problem for a full day. This will be okay:

DEBUG = True
ALLOWED_HOSTS = []

Upvotes: 3

Mohamed Abou Masry
Mohamed Abou Masry

Reputation: 1

You don't have to refer to STATIC_ROOT = 'staticfiles'

Just Like that:

STATIC_URL = '/static/'

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

Upvotes: 0

Raja Simon
Raja Simon

Reputation: 10315

Django default BASE_DIR will search static content for you. But in your case you changed the way before initial project. So for that in your case you have to change your BASE_DIR like this .. then only static file will serve correctly

BASE_DIR = os.path.dirname(os.path.abspath(__file__) + '../../../')

Updated:

I didn't see that comment. ! DEBUG = True only for development and You set as False so will django use STATIC_ROOT = 'staticfiles' to serve the static content on the production environment ... Thank you

Upvotes: 1

doniyor
doniyor

Reputation: 37934

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.

Upvotes: 5

hlkstuv_23900
hlkstuv_23900

Reputation: 882

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.

Upvotes: 7

Related Questions