views:

151

answers:

2
background: url({{ MEDIA_URL }}/bg.jpg);

That does not work, because this Django template function only works in .html!

+8  A: 

Django template function only works in .html!

This is totally wrong. Django templates are independent of the format. It can be easily used on any other format, infact for many other purposes.

how can I embed something in CSS?

You serve your css from a static server like nginx or apache (configured for statics); Django does not even access these files. Only during development, for sheer convenience, you can ask django to serve statics too.

In django templates, you need to load the template and render a context. For convenience, render to response does that, and returns it as a http response. If you need to edit a css, you will need to do the loading and rendering as well. When you do this, it might be easier to embed an inline style within the page than importing from an external css file.

Lakshman Prasad
To this, I add: remember to set text/css as content type in the HttpResponse.
Stefano Borini
I think the OP was trying to put a Django template directive inside a static CSS file. If he had been generating the CSS at runtime with a template< you'd be right. Of course, that would be a pretty doubtful use of Django templates.
hughdbrown
+9  A: 

Actualy image in CSS can be relative to the CSS file. So you don't really need to put the MEDIA_URL in a CSS file.

Natim