-2

I am trying to setup a static url in Django and have my templates use it to link to the css. Here is my code. Why is it not working? Also, what is the best practice for setting this up? Thanks.

# settings

import os

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

STATICFILES_DIRS = (

)

INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    )


# urls

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of my URLconfs here ...

urlpatterns += staticfiles_urlpatterns()


# html template

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

# file structure
-project
--project
---settings
---static
----css
-----style.css
0

1 Answer 1

1

Your STATICFILES_DIRS needs to include a list of paths to your static directories for development. The STATIC_ROOT setting is used for collecting static files into a single directory in production to be served by HTTP server such as Nginx or Apache.

Seeing as you have already defined a PROJECT_ROOT you could do this:

# Assuming your settings.py is at the same level as your static directory
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'), ]
Sign up to request clarification or add additional context in comments.

3 Comments

Still not working for me. Any more tips?
try @catherine's answer too.
Ok something else must be wrong because even when I link it with the exact file path to the stylesheet it's not using the css, but I tested it on another file outside of django and it worked fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.