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?

1 Answer 1

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" %}"
Sign up to request clarification or add additional context in comments.

5 Comments

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
It shouldn't matter. My point was that you should add the app name to the url.
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...
This answer should be useful: stackoverflow.com/a/40929708/2419215
I added STATIC_ROOT to settings.py (check updated question), then ran python manage.py collectstatic. But I still get these 404s...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.