oyvindym
oyvindym

Reputation: 352

.css won't affect my template in django

I am currently writing my first django-app and are encountering a few problems, as expected.

The problem I am struggeling with at this moment is linking css to my template files. I've been searching around stackoverflow, but nothing of what I find seems to work.

I am currently running django 1.4.2

In my settings, I have staticfiles app and staticfiles_finders enabled. My staticfiles_dirs looks like this:

STATICFILES_DIRS = (
    '/home/user/project/static/',
)

Inside my static folder I have a css folder where I store my css files. In this case the path to my css file is this.

/home/user/project/static/css/index.css

In my template for my app, I've linked to the css file like this.

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

However, the css does not seem to affect my page. The css does however work if I write it directly in my template file so I assume I've linked it wrong?

Help would be much appreciated.

Upvotes: 0

Views: 574

Answers (2)

asesino
asesino

Reputation: 66

You can try another way :

Edit these variables in the file settings.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = os.path.join(PROJECT_URL, 'static')

They are refering to absolute path to folders on your system, for example :

  • PROJECT_ROOT --> /home/user/project
  • STATIC_URL --> /home/user/project/static

Expand url from STATIC_URL variables :

  • {{ STATIC_URL }} --> h ttp://(your_host)/static/
  • {{ STATIC_URL }}/css --> h ttp://(your_host)/static/css

Upvotes: 0

catherine
catherine

Reputation: 22808

Django 1.4+

{% static %}

<link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}"/>

Note: If this code is not working, you should check your settings and urlconf. And I also notice some bad practice in your codes.

Upvotes: 1

Related Questions