0

I've been tasked with fixing some aspects of a django 1.3 site, that runs on Apache/PostgreSql on the server. I'm trying to set it up on my development machine, with a virtual environment with postgres and the internal python developer server.

I managed to get the site running and reading from my local pg instance, but I can't get it to recognize my static files, which are stored in /site_media. The site will soon be rewritten to use django 1.6 and the proper /static folder but for now I need to fix this site.

I also tried running it with nginx and gnunicorn but the result is the same, the site displays but with no style and all references to files in the static dir give a 404. further inspection of the 404 reveals that django is trying to resolve the resources with its router.

Here are the relevant settings:

settings.py:

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/nico/src/df2/datos/site_media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://localhost:8000/site_media/'

I also added the following config, to no avail:

INSTALLED_APPS = (
    'django.contrib.staticfiles',
)

STATICFILES_DIRS = (
    '/home/nico/src/df2/datos/site_media/',
)

STATIC_URL = 'http://localhost:8000/site_media'

nginx config file:

server {
    server_name localhost;

    access_log off;

    location /site_media/ {
        alias /home/nico/src/df2/datos/site_media;
    }

    location / {
            proxy_pass http://127.0.0.1:8001;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

If you need any other config please tell me.

I'd prefer to run this purely from the python server, but a solution with gunicorn/nginx is also fine

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42
  • 1
    Have you read through [the docs](https://docs.djangoproject.com/en/1.3/howto/static-files/) and run `collectstatic`? – jonrsharpe Nov 28 '13 at 17:24
  • In particular, have you added `staticfiles_urlpatterns()` to your urlconf? – Daniel Roseman Nov 28 '13 at 17:51
  • @jonrsharpe I can't use that because the root with the static files is already configured as media_root and I can't change the directory structure to put the static files where they should be (like I said in the post, this is a future step; for now, the site needs to be left as is) – Nicolas Straub Nov 28 '13 at 18:52
  • @DanielRoseman I tried that but nothing happened. I'll look into it though... thanks! – Nicolas Straub Nov 28 '13 at 18:52

1 Answers1

0

The fix was in adding a static handler to the urlpatterns variable:

from django.conf import settings

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))
Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42