4

It's been a while since I've setup django to work locally. I'm using version 1.11. Getting it to serve the static files. My project is called chatsys and I've created the static folder and css in this folder chatsys\static\css\style.css .

Here's the current settings in the settings file.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

and in the urls

#for serving static files
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and finally in the html

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

however in the runserver console I get 404 for /static/css/style.css

4
  • Are you running in development with DEBUG=True? Where is the style.css file located? If you print/log STATIC_ROOT, does it give you the correct location? Commented Apr 17, 2018 at 9:24
  • yes, debug = True and the css file is located in chatsys\static\css\style.css which is in the main directory. Printing the path looks ok... it's <full_path>\chatsys\static Commented Apr 17, 2018 at 9:28
  • I'm still not completely clear on your project layout. Is the STATIC_ROOT in the project directory (the one containing manage.py) or the inner directory (the one containing settings.py)? Your STATIC_ROOT is set up for the first case. Commented Apr 17, 2018 at 9:31
  • in the project directory containing manage.py Commented Apr 17, 2018 at 9:38

2 Answers 2

5

You should define STATICFILES_DIRS and include your project's static directory there.

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

These are the directories that Django collects static files from.

You should then change STATIC_ROOT to be a different directory. It is the directory that collectstatic collects static files to. The static root should not be under version control.

As an aside, you are loading the static tag in your template but not using it. You could change it to:

{% load static %}
...
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
Sign up to request clarification or add additional context in comments.

3 Comments

on point, you nailed it :) I'll stick to not using load static I was trying to use it to debug and see if that would fix it.
At the moment, it doesn't really matter whether you use {% static %} or hardcode /static/. But if you change STATIC_URL in future (e.g. to use a CDN) then you won't have to update all your templates if you used {% static %}.
I know, and its just for a project trying to see if I can make a chatting sys so likely won't be hosted. And good thing still is I tend to use the same path in nginx anyway.
0

Move your static folder under the base dir

  • chatsys
    • migrations
    • templates
    • etc.
  • static
    • css
      • style.css

3 Comments

Could you please try to remove {% load static %} ?
tried it, didn't work. it's part of the configuration that has an issue.
After the STATIC_ROOT try to add this line STATICFILES_DIRS = [ STATIC_ROOT, ] .

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.