1

I followed some tips from related questions in here, and tried the documentation but failed so far to get css file working.

Note that css (obviously) works perfectly fine if I place it within the template, but I can't import it from static folder.

So in my app folder, I created a static folder, within which I created another folder called static: app->static->style.css

When I run collectstatic, it gets style.css, and places it in: projectfolder->static->style.css

All of this is working fine.

My static settings were set automatically, I changed it based on some answers I read in related questions, but still didn't work:

MEDIA_ROOT = u'/home/user/projectfolder/media'
MEDIA_URL = '/media/'
STATIC_ROOT = u'/home/user/projectfolder/static'
STATIC_URL = '/static/'

In my template, I have:

{% load staticfiles %}
....
<head>
        <link rel="stylesheet" href="{{ STATIC_URL }}style.css" />
....

Clearly I'm missing something, when I reload the website, no css is shown. In my css I have clearly set the body background to test it, but it doesn't change.

Extra info: - Debug is set to true - Using the latest Django version .10

Any help or direction would be appreciated,

thanks in advance.

3
  • 1
    Are you doing this locally or on a server? I assume locally because of your DEBUG setting, but wanted to double check. Also try changing your href to "{% static 'myapp/style.css' %}" where myapp is the app name of course. Commented Aug 26, 2016 at 13:58
  • @elethan This is done on a server, I tried "{% static 'myapp/style.css' %}" and it didn't work, then I tried "{% static 'style.css' %}" and it worked. Thanks mate, place it as an answer and I will choose it as correct. Thanks again :) Please leave a little explanation for others who might be facing the same problem. Commented Aug 26, 2016 at 14:06
  • you might as well accept the posted answer if you are satisfied with it, and I will not bother posting my own. Glad you got things sorted! Also, since you are talking about a Django site on a remote server, be aware that when you update static files there, the changes will not always be immediately visible on your live site. I have gotten tripped up by this a few times, and recently answered a question about this here: stackoverflow.com/a/39126082/3642398 Commented Aug 26, 2016 at 14:13

1 Answer 1

2

When you view the page's source code from what path is it trying to load your style?

Try this - <link rel="stylesheet" href="{% static "style.css" %}" />

Also I would change MEDIA_ROOT and STATIC_ROOT to MEDIA_ROOT = os.path.join(BASE_DIR, 'media') and STATIC_ROOT = os.path.join(BASE_DIR, 'static'). This way both paths are being built by the system and it's harder to mess up.

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

1 Comment

I edited the question, it wasn't app->static->app->style.css, rather it was app->static->style.css. I took elethan's advice and it worked. I will also consider your advice and change the media_root and static_root. Thanks.

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.