Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using template language in CSS files correctly

I'm attempting to serve a CSS file through my url.py file so I can use template tags in my CSS.

Here's my code.

Base.html:

<head>
...
<link rel="stylesheet" type="text/css" href="/site_media/css/wideTitle.css" />

urls.py:

(r'^site_media/css/wideTitle.css','lightbearers.views.viewWideTitle'),

views.py:

def viewWideTitle(request):
    contentListAbout = About.objects.filter(Q(startDate__lte=datetime.now())).order_by('-startDate')
    if len(contentListAbout) > 0:
        contentAbout = contentListAbout[0]
    else:
        contentAbout = None
    ...
    return render_to_response('wideTitle.css', {'contentAbout':contentAbout, ...},
                              context_instance=RequestContext(request))

settings.py:

TEMPLATE_DIRS = (
    os.path.join(PROJECT_PARENT, "templates"),
    os.path.join(PROJECT_PARENT, "media/css"),
)

wideTitle.css (in /media/css):

#wideTitle{
    clear:both;
    height:180px;
    padding-left:0px;
    background: url({{ contentAbout.headerImage.url }}) no-repeat top left;
}

I can access the CSS file by entering its URL in my browser, but the Base.html isn't reading it at all. I think I've got everything decent; I've looked here and here for tips. Anyone have ideas?

like image 606
reK1NDLE Avatar asked Nov 04 '22 05:11

reK1NDLE


1 Answers

Is the generated stylesheet being served with the correct mime type? If not, the browser might not interpret it as CSS.

I can’t remember if render_to_response accepts content_type='text/css as an argument, but there is a way to set it if Django isn’t already using the correct mime type.

Edit: as @TommasoBarbugli pointed out, you want the mimetype argument for render_to_response.

(Firefox’s Firebug add-on, or the Web Inspector in Chrome/Safari, should be able to show you the stylesheet’s mime type.)

like image 139
Paul D. Waite Avatar answered Nov 08 '22 00:11

Paul D. Waite