0

I know this is a stupid question, but I've read through the Django documentation a dozen times and read every related question on on here and still can't figure out what I'm doing wrong.

I'm trying to link my template CSS to a file in my static root. Can someone tell me what I'm doing wrong?

settings.py:

STATIC_URL = '/static/'
STATIC_ROOT =  'C:/Users/Chris/Dropbox/MyProject/MyProject/static/styles/'

STATICFILES_DIRS = (
    "C:/Users/Chris/Dropbox/MyProject/MyProject/static/styles",
    )

in the template:

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

urls.py

from django.conf.urls.static import static

urlpatterns = patterns('',

# my url patterns here

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

My guess is that either the static files code in urls.py is incorrect or the STATICFILES_DIRS in settings.py, but I've tried a million combinations based on other questions on stackoverflow and nothing seems to work. I just get a 404 page not found error.

Any help is appreciated. I've spent an embarrassing amount of time on this.

0

3 Answers 3

2

You have two main problems:

  1. Your STATIC_ROOT directory is included in STATICFILES_DIRS. STATIC_ROOT is where the collectstatic command will copy all static files, once you are ready for deployment. You obviously don't want this path to be any where you already have static files as all files in this path are overwritten.

  2. Your static url is including a path component already included in STATICFILES_DIRS. You have /MyProject/static/styles in STATICFILES_DIRS, this means that the static tag will look inside this directory for requested files. Now you are requesting a link to '/styles/style.css', and so django is looking for this file: /MyProject/static/styles/styles/style.css which does not exist. To fix the problem, change the static tag to {% static 'style.css' %}.

Sign up to request clarification or add additional context in comments.

1 Comment

The main problem through all my attempts was the duplication of the '/style/' directory. Thank you so much. Such a relief to have it working now.
0

try this in settings.py file

STATIC_ROOT = os.path.join(SITE_ROOT, 'templates/static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (

                         SITE_ROOT + '/templates',

)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)

and in urls.py file

urlpatterns += patterns('',
                        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                         {'document_root': os.path.join(settings.SITE_ROOT, 'templates/static')}),

                    )

hope this will help you

Comments

0

Using static path maybe more intuitive.

In settings.py declare

STATIC_PATH = os.path.join(BASE_DIR, 'static')
# Add any additional locations of static files

STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
# os.path.join(ROOT_PATH, "public")
    STATIC_PATH,
)

Then in your template simply load the files

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.