Sam B.
Sam B.

Reputation: 3033

Django serving static files locally

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

Upvotes: 4

Views: 6404

Answers (2)

Alasdair
Alasdair

Reputation: 308769

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' %}">

Upvotes: 5

Panos Angelopoulos
Panos Angelopoulos

Reputation: 609

Move your static folder under the base dir

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

Upvotes: 0

Related Questions