Reputation: 53
I'm working with an existing (and previously functional) Django site. We recently upgraded from Django 1.8.13 to 1.10 and our WSGI is Gunicorn. It works fine when hosted from my development machine, but when deployed, all static resources (on the admin and the main site) yield 404's with the message, Directory indexes are not allowed here.
Our settings.py
contains the following:
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
DEBUG = True
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_resources')
The directory structure looks like this:
/my-project-name
/my-project-name
server.py
settings.py
urls.py
wsgi.py
...
/static
/static_resources
manage.py
Upvotes: 0
Views: 368
Reputation: 53
The answer was very subtle. When I upgraded Django to 1.9 and ran the server, it gave the following warning:
?: (urls.W001) Your URL pattern '^static/(?P<path>.*)$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
In urls.py
, my urlpatterns
list contained:
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
I changed it to:
url(r'^static/(?P<path>.*)/', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
This eliminated the warning but caused static resources to stop loading. It needed to be:
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
It's still a mystery to me why this worked on my dev machine (a Macbook), as well as another on the team's dev machine (a windows laptop), but not on our Linux server. But, it works now, so I'm done trying to figure it out.
Upvotes: 0
Reputation: 14404
Django does not serve static files in production mode (DEBUG=False). On a production deployment that's the job of the webserver. To resolve the problem:
python manage.py collectstatic
/static
folder to the static folder of DjangoDon't just turn DEBUG on, it would be dangerous!
Upvotes: 1
Reputation: 26
Try to change os.path.join(PROJECT_DIR, '../static') to os.path.join(PROJECT_DIR, 'static') and STATIC_ROOT = os.path.join(PROJECT_DIR, '../static_resources') to STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_resources'). It will solve your problem.
Upvotes: 0