1

In settings.py, part of my code sets the static url.

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

My static files and folders are at /home/myLinuxUsername/myApp/static/interactive-preview-dependencies. That has the folders containing CSS, JS and images. It has the folders images, scripts and stylesheets.

My urls.py's urlpatterns is like this:

urlpatterns = [
    # .. SOME CODE HERE .. 
    url(r'^interactive/$', interactive),
]

My views.py is like this:

def interactive(request):
    t=  get_template('interactive/index.html')
    return HttpResponse(t.render())

And here's an example of how I try to load CSS on interactive/index.html (updated):

<link rel="stylesheet" href=static 'interactive-preview-dependencies/styles/main.css' %}">

When I run python manage.py runserver and go to localhost:8000/interactive, the terminal gives me this error: "GET /static/interactive-preview-dependencies/styles/main.css HTTP/1.1" 404 1757

How do I fix this so Django finds and loads the CSS?

Username
  • 3,463
  • 11
  • 68
  • 111

1 Answers1

1

If I understand correctly, your app is called myApp and interactive-preview-dependencies is just a subfolder inside /home/myLinuxUsername/myApp/static/. According to Managing static files. You should reference it as

... href="{% static "myApp/interactive-preview-dependencies/styles/main.css" %}"
fodma1
  • 3,485
  • 1
  • 29
  • 49
  • How would this work if I want to work on this on a different machine, especially a Mac or Windows? The absolute path won't be the same each time – Username Mar 04 '17 at 22:37
  • It shouldn't matter. My point was that you should add the app name to the url. – fodma1 Mar 04 '17 at 22:45
  • Ah I see. I edited the relevant code, but I get this 404 error: `"GET /static/myApp/interactive-preview-dependencies/styles/main.css HTTP/1.1" 404 1781`. Looks like Django is looking in a different place now... – Username Mar 04 '17 at 23:07
  • This answer should be useful: http://stackoverflow.com/a/40929708/2419215 – fodma1 Mar 04 '17 at 23:11
  • I added `STATIC_ROOT` to `settings.py` (check updated question), then ran `python manage.py collectstatic`. But I still get these 404s... – Username Mar 04 '17 at 23:23