0

I have spent the last few days figuring out how to include an css file into a Django template. I still did not succeed so am hoping someone can help me out. I have the following settings:

--settings.py--

MEDIA_ROOT = '' 
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'

I have not set anything in STATICFILES_DIRS() either.

--urls.py--

urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += staticfiles_urlpatterns()

--views.py--

def my_homepage_view(request):
return render_to_response('test.html', context_instance=RequestContext(request))

--test.html template--

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>

--source code localhost--

<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>

According to the Django documentation it seems that I have set everything correctly, but the css style is still not applied. The static folder is in the correct place (C:reviews/reviewsite/static) where the rest of my apps also reside. Even if I hardcode the style.css location (C:reviews/reviewsite/static/css/style.css) in the test.html template the css style is not applied. I have checked the style.css and it works without Django. Any idea of what I am doing wrong?

  • Can you post your application's folder structure and specify where your static folder is located in that? Refer [this answer](http://stackoverflow.com/a/15130566/1095090) and try it. It may help you. – arulmr Mar 11 '13 at 09:19

2 Answers2

0

This is how you call the files in the static

{% static %}

<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>

settings.py

import os
import sys

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT

MEDIA_ROOT = os.path.join(SITE_ROOT, 'media') 
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    //create staticfiles folder
    os.path.join(SITE_ROOT, 'staticfiles'),
)
catherine
  • 22,492
  • 12
  • 61
  • 85
0

Everyone, thanks for your help and sorry for the late reply. I tried your suggestions but it didn't work unfortunately. However, after some more time trying I got it working now. This is what worked for me:

--settings.py--

MEDIA_ROOT = ''
MEDIA_ROOT = ''
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = 'http://localhost:8000/static/'

--urls.py--

urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }), )

I now have placed the css file in the static folder in my apps directory. In the template I am using {{ STATIC_URL }}/style.css.