3

I've been looking around everywhere and trying everything but i cannot seem te get my css file to work in a Django template. My css i called style.css the code in the template right now looks like:

{% load staticfiles %}

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

I'm still working on the development server. In settings py I added: STATICFILES_DIRS = ( "home/henk-jan/website/Template/Database")

django.contrib.staticfiles is installed in Installed_apps

can anyone help me with this? Cheers, Henkes

Edit: My template (index.html) is in the same folder as my style.css the folder is: /home/henk-jan/website/Template/Database

Henkes
  • 288
  • 4
  • 16

3 Answers3

3

From looking at your original post it would appear to me that your working at rendering your page from two separate angles.

First, you have {% load staticfiles %} which will load the templatetags associated with the staticfiles module. Second, inside your link element you are referencing {{ STATIC_URL }} which gets expanded via context.

From this I would recommend one of the following two courses of action.

1 - Utilize the staticfiles module and the templatetags you loaded in your template.

To do this you should modify your link element to read like this:

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

Note that in this instance I have replaced the {{ STATIC_URL }} with the {% static %} templatetag. The {% static %} templatetag takes an argument which is the file you wish to prefix with the static URL, and expands into the complete string.

2 - Make use of context by modifying your view to render with context.

The {{ STATIC_URL }} variable is made available via request context. There are a number of useful variables that are, that you can rely on to get expanded if you want to utilize them. The trouble is that you have to ensure that you render your template with context which means you would potentially have to change one or more views.

As an example an overly simple view that renders without context would look like:

from django.shortcuts import render_to_response

def index_without_context(request):
    return render_to_response("index.html")

While the same overly simple view rendered with context would look like this:

from django.shortcuts import render_to_response
from django.templates import RequestContext

def index_with_context(request):
    return render_to_response("index.html",
                              context_instance=RequestContext(request))

As I stated above, by rendering your template with a RequestContext you get other variables and such that can be of use so it is a very viable option.

In the end it really depends on where you want to keep the logic that ensures your static files get your static URL rendered correctly at. If you want that logic inside the template itself I would recommend you go with the {% load staticfiles %} approach and use the {% static %} template tag. If you prefer to make use of the {{ STATIC_URL }} variable as well as having other potentially useful variables available then I would recommend modifying your view to be rendered with a RequestContext.

You can read more about the difference between using the context processor or a template tag in the Django docs section about this very topic:

https://docs.djangoproject.com/en/1.4/howto/static-files/#referring-to-static-files-in-templates

zzzirk
  • 1,582
  • 2
  • 14
  • 18
  • First of all, I knew I had 2 different types of static content loading, It was there because I tried everything! In the end I found out what the problem was. It went wrong with working on the development server (I was working on viritual machine) I switched to server and it worked! Thank you for your very nice and detailed solution! – Henkes Oct 15 '12 at 09:38
1

Is "home/henk-jan/website/Template/Database" a valid location? Maybe "/home/henk-jan/website/Template/Database" instead? Right now the preceding forward slash is missing.

Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
0

If you are working on the development server, you will want to let Django serve the static content. When you go to production you will have your web server handle serving static content instead.

You will want STATIC_URL pointing to the path to your static content (in this case it looks like that would be /Template/Database/. It sounds like you have that working. Now you just need to tell Django to serve static content when in DEBUG mode. See this post: Django MEDIA_URL and MEDIA_ROOT

Community
  • 1
  • 1
Micah Carrick
  • 9,967
  • 3
  • 31
  • 43
  • Carric I tried that as well, I had the URL already in URLS. still nothing. I am very confused about that because it has to work... Maby because I'm working on a viritual machine? but I don't think that's it because i've never had any problems with that before.. – Henkes Oct 03 '12 at 13:56
  • If STATIC_URL renders correctly when you view the source of your page, then you can suspect that Django doesn't know how to serve your static content. Confirm this by navigating to the CSS file directly in your browser to ensure it is a 404 error. If that is the case, look at which URL patterns it tried to make sure the pattern to find your CSS file exists. It should exist because you should have (1) set DEBUG=True while in development, (2) have `urlpatterns += staticfiles_urlpatterns()` in your urls.py, and have the path to the CSS file in your STATICFILES_DIRS. – Micah Carrick Oct 03 '12 at 14:59