6

I'm just newbie in django. Just learning. Want to know how to make best loading of css into django template. Work in django-env with django 1.8

So, how I'm trying

settings.py

STATIC_URL = '/static/'    
STATICFILES_DIRS = (
    ('static', '/home/djangoenv/bin/mysuperapp/static'),
)

my_template

{% load staticfiles %}

<!DOCTYPE>
<html>
    <head>
        <title>Django</title>
        <link rel="stylesheet" type="text/css" href='{% static "/css/foundation.css" %}'>
    </head>
    <body>
        <h1>Django</h1>
    </body>

</html>

In this case I have no loaded css and a warning message in Chrome Console:

Resource interpreted as Stylesheet but transferred with MIME type
text/html: "http://127.0.0.1:8000/css/foundation.css".
2
  • Just add a base static dir to your STATICFILES_DIRS setting, then let the app static loader take care of app specific static folders. Kinda like; STATICFILES_DIRS = (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'),) Commented Apr 20, 2015 at 23:12
  • @marksweb, There is no effect. =( Commented Apr 20, 2015 at 23:35

1 Answer 1

3

Remove 'static' in STATICFILES_DIRS

STATICFILES_DIRS = (
    '/home/djangoenv/bin/mysuperapp/static',
)

This static creates separate namespace, so you would have to call {% static "static/css/foundation.css" %}.

And use static templatetag without leading slash: {% static "css/foundation.css" %}.

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

5 Comments

Yeap, I've already checked. But I still don't understand where is the fault.
I've tried. Now I get error "404". If use in template {% static "/css/foundation.css" %} have again the same warning.
Do you have DEBUG set to False in settings? If yes, you have to call python manage.py collectstatic.
It say You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. What I should do?
Set STATIC_ROOT in settings. This is folder where Django collects static files. docs.djangoproject.com/en/1.8/ref/settings/…

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.