[Fixed]-Django: css referencing media in static files (django dev / 1.3 / static files)

19👍

You said you had trouble with relative paths, but I don’t understand exactly what you meant.

I ran into the same issue, and I’ve used relative paths to solve it. The only thing to keep in mind is that when deploying the images need to (obviously) remain in the same path relative to the CSS files.

My setup in a nutshell:

Note I’m still using django-staticfiles with Django 1.2, but it should work similarly for Django 1.3

STATIC_URL = "/site_media/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, "static_media"),
)

Then I serve the CSS from {{ STATIC_URL }}css/style.css which references images at ../images/logo.png.

and my project looks like this:

project_dir
  ...
  stuff
  static_media
    ...
    css
    images

Let me know if you have any questions, and I’ll clarify.

1👍

Ok,

I don’t know if there is something wrong with @John’s solution but it didn’t worked to me then I put this code on the CSS

{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

and

<link rel="stylesheet" href="{{ STATIC_PREFIX }}css/main.css">

Hope it helps!

👤Mc-

Leave a comment