0

Okay, all. I know this is a question that many people have solved in various cases, but I cannot for the life of me get my Django 1.9 development server to serve static content on my local computer. Static files worked fine pre-deployment, and are totally fine on my deployment server, but now in my test environment (local computer with runserver going) everything is broken, and I really need to be able to test stylesheets in a dev environment.

I have tried all of these solutions and more, followed the documentation guide, used collectstatic again in the development repo... nothing has worked, and I am at my wits' end.

Currently, I have DEBUG = True, and the following setup:

Folder Hierarchy

project/
   manage.py
   (&c)
   app/
     urls.py
     models.py
     (&c)
   project/
     settings.py
     urls.py
     (&c)
   static/
     styles/
     images/
     (&c)

settings.py Static Files Settings

STATIC_URL = '/static/'

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

STATICFILES_DIRS = (
    os.path.join(STATIC_ROOT, 'styles/'),
    os.path.join(STATIC_ROOT, 'js/'),
    os.path.join(STATIC_ROOT, 'audio/'),
    os.path.join(STATIC_ROOT, 'images/'),
    os.path.join(STATIC_ROOT, 'admin/'),
    os.path.join(STATIC_ROOT, 'documents/'),
)

urls.py URL Patterns

from django.conf.urls import include, url, patterns
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin

urlpatterns = [
    # ... project url patterns blah blah ...
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT, 'show_indexes': True}))

I also have {% load staticfiles %} in my templates along with the appropriate {% static %} calls (ex: {% static 'styles/main.css' %}).

For reference, the command line gives me the following when I load the page:

"GET /static/styles/main.css HTTP/1.1" 404 1759

If anyone knows of a fix I have missed that might even remotely have a snowball's chance of working, please let me know. It's driving me bonkers not being able to test properly.

EDIT: As suggested, I have updated to Django 1.11 on my local machine, with no changes to the current issue.

2 Answers2

0

I'm not able to test it out, but maybe try changing this:

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

to this:

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

By the way, version 1.9 is unsupported and also a security risk. You'd probably want to upgrade to another version.

LizD
  • 359
  • 4
  • 6
  • 2
    No dice. As far as I know, `os.path.join()` appends & inserts the necessary slashes (which is why it's preferred over simple string concatenation). Upgrading is my next priority. – Jensen Reynolds Jun 14 '17 at 02:00
  • Darn. Sorry about that. Good luck! – LizD Jun 14 '17 at 03:42
0

Can you try to create the STATIC_ROOT var with the absolute path and check if it works?

Maybe the construction of that var is not OK.

You could also check the STATIC_ROOT var through

python manage.py shell

and see if it correctly fits your path.

jgmh
  • 639
  • 10
  • 22