tags:

views:

41

answers:

1

i want to create some css styles for my Django templates. Each view will have a css associated, but there will be zones that are not associated to any views in my template.

How can i load the css files? is there enough having them declared in the Media of my view, and loading them in the header of the html?

Also, is it a correct approach to have styles associated to the divs that are not associated to a certain view?

Thanks!

+3  A: 

If you use a word view for a typical django view (a method) it is not good idea to create separate css file for each view (unless you have very specific application).

In general you need to create you css files in such way that:

  1. general styles that can be applied in many templates are not repeated in multiple css files
  2. it is easy for you to manage styles in couple of css files

There is no strict guideline to create css file per view or css file per template in Django as far as I know.

Basically pointing to some css file in head, which contains styles appropriate to a template is enough here. Of course you need to make sure that you provide correct path to this file.

You can make one general css with styles that are used by most of your templates and a series of specific css files that are valid only in some specialized templates.

I also advice to take a look at django-compress if you want to go with your site to broader audience - this app makes your static files (like css) smaller and also it helps to concatenate group of css and js files. This has some positive impact on performance without decreasing readability of your code.

Lukasz Dziedzia
hmm.. i see. So, i can use the classic <div class = 'somecss'> in the django tempaltes too, it is not deprecated, right?
dana
I don't think it is deprecated. Do you know some different approach? If you're starting with css+html I highly advice to apy atention to duplicated styles in your css files - it can do a lot of mess! I also advice to take a closer look on css files organization in some bigger (and nice looking) projects.
Lukasz Dziedzia
perfect then, i'll do so! thanks so much for support!
dana
There is another reason for combining your CSS into one file: load time. Loading a single 48KB file is, overall, *much* faster than loading 4 12KB files or (horrors!) 12 4KB files. Most of the cost of the file load is making the network connection. This also applies to other types of support files, e.g. JavaScript and icons. JS files can be merged, and icons can all be put in one image and then use the CSS sprite feature to select the portion of the image you want. (Here is one of many tutorials on the subject: http://css-tricks.com/css-sprites/)
Peter Rowell
This is exactly what I had on my mind when I wrote aboute 'impact on performance. django-compress is a helpful tool here
Lukasz Dziedzia