0

I'm following the official documentation in order to serve static files but I'm recieving error 404 in the development console. I'm using 'django.contrib.staticfiles', so static files should be automatically served. This is my setup:

Settings:

STATIC_ROOT = ''
STATIC_URL = '/static/'

Template header:

{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/app.css" %}">

Directory tree:

django_project
    \apps
    \static
        \css
            app.css
    \templates
        index.html

I can see in firefox console that the path to my file is correct:

enter image description here

So the problem must be that Django is not serving static files. I can't find what I'm missing. Any advice is more than welcome.

govin
  • 6,445
  • 5
  • 43
  • 56
Adrian Lopez
  • 2,601
  • 5
  • 31
  • 48
  • I think the code has a typo : `"{% static "css/app.css" %}"` , you should replace one pair of double quotes with single quotes. – jeff Apr 13 '15 at 18:54

4 Answers4

6

SOLUTION: I was missing this line in my settings.py

STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static'),)

It looks it's mandatory, same as TEMPLATE_DIRS.

Adrian Lopez
  • 2,601
  • 5
  • 31
  • 48
  • I also tried to use STATIC_ROOT, but it's deprecated in this version of Django, so only STATIC_URL, STATICFILES_DIRS and DEBUG=True need only. – Vladimir Chub Feb 28 '15 at 17:45
0

When you run collectstatic it puts all your static content in the path specified by STATIC_ROOT. Check the deploy to production docs

If you are using the django server try checking the path that is being generated by {% static %}...you may have some trailing slash or something missing.

Check that you have are following all the requirements. You need to have django.contrib.staticfiles in your installed apps and something like this in your main urls file:

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

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
cdvv7788
  • 2,021
  • 1
  • 18
  • 26
0

That should work :)

settings.py

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

example of context_processors from settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    'django.core.context_processors.request',
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
)

example of installed apps in settings.py:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

urls.py:

from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
    urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Efrin
  • 2,323
  • 3
  • 25
  • 45
  • 2
    Actually the only things that you need to serve static files in 1.7 are STATIC_URL, STATICFILES_DIRS and 'django.contrib.staticfiles' in your settings. Then you only need to use any static file in your template like {% static "css/app.css" %}. So you don't need to explicitly serve static files in urls.py like in previous versions, fortunately. – Adrian Lopez Oct 28 '14 at 04:27
0

For anyone running django-cms and experiencing 404 errors (particularly, all of your static files are having "en-us" prepended to the URL), I found the following steps to help.

First, turn off internationalization of pattern matching in your urls.py file, as described here:

urlpatterns = i18n_patterns('',
      url(r'^admin/', include(admin.site.urls)),
      url(r'^', include('cms.urls')),
)

should instead be:

from django.conf.urls import patterns

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

The import is important, because the configuration of django-cms removes the patterns import from django.conf.urls.

This solved the redirect, but it still wasn't finding my static files. I needed to manually add the static url to the url patterns, like this:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

After that, static files worked as expected.

I'm sure that this is probably related to me messing up my configuration as a complete novice to Django. But since others might have the same problems, I'm putting it out there as a possible, if less than ideal, solution.

Community
  • 1
  • 1
tobylaroni
  • 843
  • 8
  • 16