0

My link looks like this:

<link rel="stylesheet" type="text/css" href="{% static 'css/background.css' %}">

This is fine because it gets the path I want which is href="/static/css/background.css". I use {% load static}. I have django.contrib.staticfiles in my INSTALLED_APPS. This is some more relevant info on my settings.py:

STATIC_URL = '/static/'

STATICFILES_DIR = [
os.path.join(BASE_DIR, "static"),
]

But I'm pretty sure this is right too. My problem is that static/css/background.css cannot be found by my local server. I know this because http://localhost:8000/static/css/background.css gives me a 404 error. I don't know why this is the case because this is my project file path:

Project

  Project

  src

  static

     css
       background.css
4
  • 1
    I'm confused: do you want static/css/background.css or static/background.css ? One is inside the css subfolder and the other is not. Your question seems to mix them up... Commented May 21, 2018 at 23:24
  • @Ralf My bad, that was a typo. Fixed it. static/css/background.css is the file path I want and what I am getting. It's just that the server can't find anything there. Commented May 21, 2018 at 23:29
  • Your whole URL should also include the subfolder, like this: http://localhost:8000/static/css/background.css. Does that also give 404 error? Commented May 21, 2018 at 23:33
  • @Ralf Yes, my bad I thought that was inferred. Commented May 21, 2018 at 23:36

2 Answers 2

3

Have you set your url paths ? Here is a snippet from the official Django documentation.

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Sign up to request clarification or add additional context in comments.

4 Comments

I think if you have debug set to TRUE you can use STATIC_FILE_ROOT="/my/path/to/static" ... make sure you run collectstatic first though ( or something like that)
@h3k I Thought we didn't need to do that if we set up our settings.py right. I thought STATIC_URL and django.contrib.staticfiles in settings would cover that.
@h3k which urls.py would I put that into? The one in my app or the one that is in my general project?
@TalhaAhmed you have to use the general project urls.py
0

I fixed my problem and I'm posting this in case anyone this can help anyone else. My problem is that I had my static folder outside my app. It turns out that my website is targeting my app and so when django was looking for the static folder it was looking for it in my app. So try putting your static files in your app and see if that helps.

Comments

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.