12

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!

vivekanon
  • 1,813
  • 3
  • 22
  • 44
  • Can you post your urls.py as a part of the question? – avenet Jan 03 '15 at 13:44
  • 4
    Is DEBUG set to True? Do you have "django.contrib.staticfiles" in INSTALLED_APPS? – Daniel Roseman Jan 03 '15 at 13:47
  • OK, I will assume you are during development, so try serving static content [this way](https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-static-files-during-development). – avenet Jan 03 '15 at 13:47
  • @avenet : Yes, its in development. I've tried that. Didn't work :( – vivekanon Jan 03 '15 at 13:52
  • @mystikacid Is your manage.py script located inside the root directory or inside project? – avenet Jan 03 '15 at 13:54
  • @avenet It's inside root (Django-Projects) – vivekanon Jan 03 '15 at 13:55
  • @doniyor : In my understanding, STATIC_ROOT is used for production to collect all the static files. So it won't matter here. – vivekanon Jan 03 '15 at 14:08
  • @DanielRoseman : DEBUG = True fixes it. Thankyou so much! Could you elaborate on this a bit? – vivekanon Jan 03 '15 at 14:11
  • The staticfiles app overrides runserver to serve static files, but only if DEBUG is True. When it's false, you'll need to configure your web server to serve them, as described in the docs you've been linked to. – Daniel Roseman Jan 03 '15 at 14:20
  • 1
    @tilaprimera, I would love to accept the answer but I'm afraid it wasn't the correct solution. If you read through the comments above you'll see DEBUG=True did the work. – vivekanon May 26 '15 at 07:57

6 Answers6

7

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.

hlkstuv_23900
  • 864
  • 20
  • 34
  • 3
    This gives me the error: `The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting`... my thoughts: http://stackoverflow.com/a/38387854/2586761 – ptim Jul 15 '16 at 04:27
5

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.

doniyor
  • 36,596
  • 57
  • 175
  • 260
3

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

DEBUG = True
ALLOWED_HOSTS = []
Eje
  • 354
  • 4
  • 8
Wu Jakey
  • 31
  • 2
1

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

Raja Simon
  • 10,126
  • 5
  • 43
  • 74
0

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

Just Like that:

STATIC_URL = '/static/'

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

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

It was the only problem.

Lily H.
  • 164
  • 2
  • 10