13

I can't seem to get my static files to load from my templates. I've followed the official documentation but I must be missing something.

My directory layout (generated by Django, most files omitted):

myproject
  myproject
    settings.py
    urls.py
  static
    css
      bootstrap.css
      main.css
  templates
    base.html
  myapp1
  myapp2
  ...
  manage.py

My settings.py:

STATIC_URL = 'static/'

I'm referencing my stylesheets like so (from my templates):

{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/bootstrap.css" %}" type="text/css">
<link rel="stylesheet" href="{% static "css/style.css" %}" type="text/css"> 

Which gives this once rendered (in HTML):

<link rel="stylesheet" href="static/css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="static/css/style.css" type="text/css"> 

Yet these links don't actually lead anywhere (when I visit them I get 404 error from Django). I feel that I could fix this by adding something in urls.py, but I thought Django did this automatically when you run the server? What am I missing?

  • 2
    Try: `python manage.py findstatic --verbosity 2 css/styles.css` to see where Django is looking for your static files. – deed02392 May 11 '16 at 09:32

8 Answers8

21

Have you defined your static files directory in settings.py ?

I'm guessing you have 'django.contrib.staticfiles', in your installed apps.

If you haven't defined your static files dir, you could by doing something like this:

import os.path

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
slow_mondays
  • 871
  • 7
  • 19
  • Yup I've got `django.contrib.staticfiles` loaded and I've got `STATIC_URL = '/static/'` in my settings file. –  Apr 15 '12 at 20:32
  • Have you filled in STATICFILES_DIRS with the absolute path to your static folder? Like the answer above said to do. – shadrx Apr 15 '12 at 20:34
  • Nope! I added the absolute OS path to my static directory and it worked. Thanks alot. –  Apr 15 '12 at 20:36
  • 1
    Sweet. If you want to read more about static files app ... https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#module-django.contrib.staticfiles – slow_mondays Apr 15 '12 at 20:40
3

This is the working solution for static/media/template access in django for windows,

settings.py

import os.path

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join('static'),
)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Amar Kamthe
  • 2,524
  • 2
  • 15
  • 24
3

My problem was solved by adding "STATICFILES_DIRS" in settings.py file

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

Check if STATICFILES_FINDERS is defined in your settings.py

https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_FINDERS

The default value of STATICFILES_FINDERS is good enough but you have 2 choices :

  • you need to have the static file inside an app and having this app in your INSTALLED_APPS

  • or you need to define STATICFILES_DIRS with your path to the static files if expect the behavior being the one of django.contrib.staticfiles.finders.FileSystemFinder

Rachid
  • 2,463
  • 1
  • 21
  • 9
1

I thought Django did this automatically when you run the server?

Why did you think that? If you've followed the official documentation, you won't have found that. Read what you have to do to serve them in development here.

There's another problem. Your STATIC_URL is a relative link, so browsers add it to the existing page URL. So if you're on page /foo, 'static/css/style.css' evaluates to /foo/static/css/style.css'.

Make sure it either starts with / - ie /static/ - or is a full URL, ie http://myserver.com/static/.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I'm running this locally, so I tried `localhost/static/`, `localhost:8000/static` and `/static/` all with the same result. When I used the `localhost` method, I simply got an `about:blank` page from Chrome. When I used `/static/`, I got another 404. –  Apr 15 '12 at 20:21
  • In response to the link you gave, I'm using the `runserver` method and my files are at STATIC_URL, which (according to the docs) means it should automatic. I also tried importing the module suggested by the link and still nothing. –  Apr 15 '12 at 20:23
0

I encountered this problem too. And I solved the problem by revising the href like this:

<html>
<link rel="stylesheet" href="{{STATIC_URL}}css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="{{STATIC_URL}}css/style.css" type="text/css"> 
</html>
0

Make sure that you have the static folder set up in the right place, that is if it is in the app folder, then you can get further clarification from this helpful resource1.

-6

My solution was DEBUG = True in settings.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
maximm
  • 1