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?
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With