I'm having trouble loading CSS for my Django template.
I have the following settings:
STATIC_ROOT = ''
STATIC_URL = '/css/'
STATICFILES_DIRS = ("/css")
INSTALLED_APPS = (
'django.contrib.staticfiles',
)
Do I need to use both static_url
and also staticfiles_dirs
?
urls.py is
urlpatterns = patterns('',
(r'^$',homefun),
)
views.py is
def homefun(request):
return render_to_response('home.html')
And the parent template is base.html which loads the css.
<link rel="stylesheet" type="text/css" href="/css/style.css" />
Your STATICFILES_DIRS = ("/css")
should actually be STATICFILES_DIRS = ("/path/to/your/css", )
(note the trailing comma — necessary because (eggs)
evaluates to the value eggs
, but the trailing comma forces it to evaluate to a tuple).
You need to use {{ STATIC_URL }}
as variable in templates, like so:
<link rel="stylesheet" href="{{ STATIC_URL }}style.css">
Official Django documentation have a good explanation about serving static files.
You must set STATIC_ROOT
variable. Using STATICFILES_DIRS
is optional, by default Django search within static
directories of all your apps.
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