3

I'm new to Django, and i'm having hard time including css styles in a template.

I read this and tried to do the same but it's not working for me.

my Template:

{% load static %}<html><head><link href="{% get_static_prefix %}/style.css" rel='stylesheet' type='text/css' /></head><body>

the HTML i get:

<head><link href="C:/Users/Nayish/workspace/am/src/am/static/style.css"rel='stylesheet'type='text/css' /></head>

Note that this is the folder containing my css.

Thanks, Boris.

Community
  • 1
  • 1
Boris C
  • 669
  • 2
  • 7
  • 14

2 Answers2

3

Make sure you haven't mixed up the STATIC_ROOT and STATIC_URL settings.

STATIC_ROOT defines where the files are on the storage system (usually your local hard disc for local development), while STATIC_URL defines the URL from where the server serves them. The second one is usually referred to in templates, and it is also the value that the {% get_static_prefix %} template tag returns.

Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
0

I am guessing you aren't using static css sheets. I always just do:

<html>
<head>
            {%block stylesheet %}
               <style type="text/css" title="currentStyle"> 
                   @import "{{MEDIA_URL}}css/style.css";
               </style>
            {% endblock stylesheet%}
   ....

I then set my Media root, and store the files as

 MEDIA_ROOT=<fullyquallified patyh>/Media/css/<css files>
 MEDIA_URL=http://localhost/mysite/

It should be noted that STATIC_URL defaults to MEDIA_URL if its not defined.

Nix
  • 57,072
  • 29
  • 149
  • 198
  • isn't Media root suppose to store user uploaded files? is that a good practice? – Boris C May 25 '11 at 14:21
  • 1
    No, site_media (or whatever you set it to) is for all your projects static resources. That includes uploads, but it's also your images, javascript, CSS, etc. – Chris Pratt May 25 '11 at 14:27
  • 1
    @chrisdpratt: that's not quite true anymore with Django 1.3's ``staticfiles`` app (http://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/). It's not wrong, either, you are not forced to use ``staticfiles``. – Benjamin Wohlwend May 25 '11 at 14:31
  • Ok, I should have phrased it better. I meant to convey simply that media root is not just for uploads as Boris though. It doesn't *have* to be for anything else (you're not required to store other static media files there), but it's not exclusive to uploads. – Chris Pratt May 25 '11 at 14:45