0

myproject_django$ gunicorn_django serves my site except for the static files. Any Idea why? I am running this from a virtual environment using django 1.3.

Project tree

myproject_django
├── core
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── views.py
├── __init__.py
├── manage.py
├── settings.py
├── static
│   ├── css
│   │   ├── base.css
│   │   ├── layout.css
│   │   └── skeleton.css
│   └── media
│       ├── pek.ico
│       ├── pek.png
│       └── pek_symbol.png
├── templates
│   └── core
│       ├── 404.html
│       ├── 500.html
│       ├── home.html
│       └── install.html
├── urls.py

Potentially relevent parts of settings.py

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

MEDIA_ROOT = ''

MEDIA_URL = '/static/media'

STATIC_ROOT = os.path.join(PROJECT_PATH,'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_PATH, 'static'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'gunicorn',
    'django.contrib.admin',
)
Bentley4
  • 10,678
  • 25
  • 83
  • 134

1 Answers1

3

You might want to configure nginx or apache to handle the static files. For now try running python manage.py collectstatic and add the below code to urls.py

urls.py

if settings.DEBUG:
    urlpatterns += patterns('',
            (r'^static/media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
        )
    urlpatterns += patterns('',
                 (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes':True}),
            )
Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • When I run `$ python manage.py collectstatic` I get this error: `django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting`. I then set `STATIC_ROOT = PROJECT_PATH` in `settings.py`. Then it didn't give any errors but when running `$ gunicorn_django` I still don't see the static files. – Bentley4 Aug 17 '12 at 13:10
  • 1
    Create a directory called `project_static` and update `STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, 'project_static'), ) ` – Pratik Mandrekar Aug 17 '12 at 13:14
  • So I created `project_static` inside `myproject_django`. Then I placed the `css` and `media` directories inside `project_static` (basically renamed my `static` directory to `project_static`). Then I implemented `STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, 'project_static'), )` in `settings.py` (next to `STATIC_ROOT = PROJECT_PATH`). Then I ran `python manage.py collectstatic` again and then `gunicorn_django`. I still don't see the static files displayed. – Bentley4 Aug 17 '12 at 13:25
  • 1
    Did you change `urls.py` to what I stated above and set `DEBUG=True` in `settings.py` ? – Pratik Mandrekar Aug 17 '12 at 13:26
  • I forgot to change `DEBUG` to `True`. Now it properly serves my css! Yay! But still not my images. If this is the ONLY way to serve static files with gunicorn alone (needing to change `DEBUG` to `true`) that basically means that I should not use this setting in a production environment. – Bentley4 Aug 17 '12 at 13:38
  • Well you could move the `urlpatterns` logic outside the `debug=true` and it would work in production. But its not recommended. Images should work too, hope you have `PIL` installed and just try debugging the paths. – Pratik Mandrekar Aug 17 '12 at 13:44
  • It works now! I just had to change my `MEDIA_ROOT` to `MEDIA_ROOT = os.path.join(PROJECT_PATH, 'project_static/media')`. Thank you! – Bentley4 Aug 17 '12 at 13:49
  • I suspected this was not the best approach. I [quote](http://stackoverflow.com/questions/10216827/django-static-files-wont-load) the user `krs`: "Some people will tell you that you need to wrap the URL-rules in a "if settings.DEBUG" to use the dev-only rules, but this isnt needed at all and actually i find that to be a bad advice." – Bentley4 Sep 11 '12 at 23:20
  • I don't agree to that. While it simplifies a lot of things while starting, using django's static file serve is not suited for production. Using a CDN or configuring Nginx to do so is the best way. So when people are ready to go beyond development, this should break and people should fix it the right way. – Pratik Mandrekar Sep 12 '12 at 06:37
  • @PratikMandrekar I think Bentley4's comment was based on Django's staticfiles view behavior: [link](https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development) it only works when DEBUG=True so wrapping with "if settings.DEBUG:" really isn't necessary. It *will* fail when DEBUG!=True. – saschwarz Nov 04 '12 at 23:18
  • Agree. Just wanted to highlight that it should not be used when `DEBUG=False` – Pratik Mandrekar Nov 05 '12 at 07:33