2

I have my little blog app, and I want to be able to change styles (not necessarily all) from admin page. What would be the 'right' way to implement dynamic style loading in django project? My own thoughts:

  • Edit css file through Python file I/O
  • Construct css file from database
  • Though both implementations have serious drawbacks. Thank you in advance for your solutions.

    Edit: I would prefer ideas not django apps :)

    Kukmedis
    • 75
    • 8
    • Possible duplicate: http://stackoverflow.com/questions/3589661/generating-dynamic-css – DrTyrsa May 16 '11 at 10:22
    • 1
      It's not impossible to render CSS just as you render templates. You could set a url that would receive in the parameters the configuration you want and would generate the css for that case. You'll have css templates and just complement them with what you need. The less.js library might also be useful, be sure to check it out! – tiagoboldt May 16 '11 at 10:26

    1 Answers1

    1

    The "right" way to do this would be to define a single class at the top-level div (or even body), which determines the master style for that page. All the style-able elements in that page then inherit this style via the magic of cascading:

    .master-default {
        color: black;
    }
    .master-default .bordered {
        border: green;
    }
    .master-blue {
        color: blue;
    }
    .master-blue .bordered
        border: yellow;
    }
    

    and so on. Now your admin interface simply allows the user to determine the top-level master style, which you then use in your base template:

    <div id="master" class="{{ userprofile.master_style }}">
        <div class="bordered">Border colour will vary according to master style</a>
    </div>
    

    etc.

    Daniel Roseman
    • 588,541
    • 66
    • 880
    • 895
    • Not necessarily - you could actually do it so that the default class is in there too, and only override the ones you actually want, with more specificity. – Daniel Roseman May 16 '11 at 10:55
    • but say I 20 different colors and want to mix it with 20 different borders. And finally what if I want to choose from whole color spectre? – Kukmedis May 16 '11 at 11:34
    • so create 20 css styles for the colors and 20 different styles for the borders. You can have multiple classes, one from each. The benefit of this is that you increase the chance of having the style cached and decrease the number of hits to your server. compressed or minified these 40 styles will add little to your page load. – newz2000 May 16 '11 at 16:45