3

I'm sorry, I know that similar questions have been asked many times... I'm really sorry to ask again but I'm having so much trouble with this (I've spent hours and hours trying everything but I just don't feel like I'm making any progress) that I thought I'd ask for some specific help.

I'm trying to set up a Django app on Dreamhost. I'm using Django 1.5 through a virtualenv which is working just fine. However, Django just cannot seem to find my static files. My setup is as follows:

My site directory:

~/distracti.co.uk $ tree
.
├── distracti
│   ├── distracti
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── manage.py
│   └── polls
│       ├── __init__.py
│       ├── admin.py
│       ├── models.py
│       ├── static
│       │   ├── images
│       │   │   └── test.png
│       │   └── style.css
│       ├── templates
│       │   └── polls
│       │       ├── index.html
│       │       └── style.css
│       ├── tests.py
│       ├── urls.py
│       └── views.py
├── django-setup.py
├── passenger_wsgi.py
├── public
│   ├── favicon.gif
│   ├── favicon.ico
│   └── quickstart.html
├── static
└── tmp
    ├── default_passenger_wsgi.py
    ├── old_passenger_wsgi.py
    ├── passenger_wsgi.py
    └── restart.txt

10 directories, 24 files

The important bit (I think) of ~/distracti.co.uk/distracti/distracti/settings.py:

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = '/home/<myusername>/distracti.co.uk/static/'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
     #... other context processors
)

...

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'polls',
)

I can then run

python manage.py collectstatic

and my app-specific static files (along with lots of Django admin static files) are collected into my STATIC_ROOT folder:

73 static files copied.

~/distracti.co.uk $ ls static/ static/images
static/:
admin  images  style.css

static/images:
test.png

Here is the top of my index.html file which is served to the browser:

{% load staticfiles %}

<head>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
</head>

<img src="{% static 'images/test.png' %}">

I can tell that this part at least is working, because the link is show in my browser as

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

The way I thought this would work is that Django would remove the STATIC_URL ('/static/') from the href string, then use the remainder of that string as a relative location to look for the file within the STATIC_ROOT directory. So given that the remainder is 'style.css', I would have thought Django would be able to find the style.css file in my STATIC_ROOT directory:

[recall STATIC_ROOT = '/home/<myusername>/distracti.co.uk/static/']

~/distracti.co.uk $ ls static/
static/:
admin  images  style.css

So the file is there waiting to be sent to the browser, in my STATIC_ROOT directory!!

However, try as I might, this just doesn't work... :( My css and png files meet with 404 errors in my browser:

GET http://www.distracti.co.uk/static/style.css 404 (NOT FOUND)
GET http://www.distracti.co.uk/static/images/test.png 404 (NOT FOUND)

Any help at all would be much appreciated. I never thought such a simple thing could be so difficult and I'd really appreciate any advice at all on this. It's so frustrating not understanding the process by which Django finds these files, can anyone help at all?

Thanks

Gareth

1 Answers1

0

You can use findstatic to check your configuration https://docs.djangoproject.com/en/1.5/ref/contrib/staticfiles/#django-admin-findstatic

Also you should try to restart your server if findstatic finds your files.

maazza
  • 7,016
  • 15
  • 63
  • 96
  • Hi, thanks for your response. I've tried findstatic and it actually does find my files, but not in the STATIC_ROOT directory, it finds them in their original static directories: ~/distracti.co.uk/distracti $ python manage.py findstatic style.css Found 'style.css' here: /home/myusername/distracti.co.uk/distracti/polls/static/style.css But for some reason my live webserver doesn't find these files, even though I've performed a restart of Django and Passenger... – user2140400 Aug 21 '13 at 16:26