2

Having written a lot of python before I'm quite new to django, and loving it so far.

My previous web experience is in PHP and in order to make my CSS easy to maintain I use a php file as my CSS and serve it as CSS content-type. Therefore I can make variables which can easily be changed to make everything in the site change.

e.g. styles.php:

<?php header('Content-type: text/css');

$pink = '#91305f';
$green = '#a4ce39';
$black = '#000000';
$white = '#ffffff';
$std_font = "'Ruda', sans-serif";
$std_shadow = "text-shadow: 1px 1px 1px #000;";
$big_shadow = "text-shadow: 2px 2px 2px #000;";
$h_space = '10px';
?>

and...

html {
    background-color:<?= $pink ?>;
    color: <?= $green ?>;
    font-family: <?= $std_font ?>;
}

so if I want to change what black means in my whole site, I just alter the $black = at the top and all places in my CSS where I've said color: ; will follow.

I want to achieve the same type of thing in django but can't figure out how to do it. I'm thinking I could use a view to render a template in css but not really sure how to go about this. Is it as simple as creating a dict in my view and passing it to render_to_response then referring to it in my template file?

e.g. views.py:

def style(request):
    colours = {'pink' : '#91305f', 'green' : '#a4ce39'}
    return render_to_response('styles.css', {'styles': colours},
                              context_instance=RequestContext(request))

then, in templates/styles.css:

html {
        background-color:{{ styles.pink }};
        color: {{ styles.green }};
    }

Is this a viable solution? If so, it seems like I am coupling the view to the template but I can't figure out where I would otherwise store the data needed to achieve this.

Thanks Aaron

3 Answers 3

1

If I understand your question correctly you want to be able to easily change the layout for your whole site, without editing alle your stylesheets. To me it sounds like you don't want to use inline styles and generated css, but instead use either SASS or LESS and use the variables to design the colors:

Sign up to request clarification or add additional context in comments.

3 Comments

fantastic! thanks @markijbema . i didn't know these sort of things exist. Given sass equires ruby, I did a quick google in the hope of keeping dependencies to a minimum and found python-scss which seems to be a python port of sass. Any thoughts/experiences on this? link
Personally I don't think that in the current age of package managers this is really a problem. Using tools like rvm/bundler for ruby, virtualenv/pip for python and npm for node, having some extra environments isn't really a problem. I personally don't know python-scss, so cannot advise on that (but scss is the good version, it's the 'new' sass)
Use something like django-compressor (django_compressor.readthedocs.org/en/latest/index.html)
1

You can do exactly what you say, so serve css from somewhere that's not in the static root, pass requests through the URLs system and render the file as a page. That's a pretty inefficient approach by all accounts though, especially if you're serving multiple CSS files per page load. What you could do in that case is to set up the web server to cache the CSS files indefinitely and only update on a command from you, in which case you'd have all the speed advantages of static CSS files and all the maintainability of dynamic ones. That approach won't work if you want to serve custom CSS to each user. The final thing you could do would be to write a little parser yourself which can parse a "meta-css" file you create where you define a load of variables like a django template, it then replaces those with variables you specify and upload the resultant CSS to be served as a static file. You could of course use the django engine to do that, just requesting the page and copying the file produced to the static directory, at which point it becomes a manual version of apache caching.

3 Comments

thanks TimD, can you elaborate on this: "set up the web server to cache the CSS files indefinitely and only update on a command from you". You are correct, it would only need to change when I decide to change a colour site-wide so it wouldn't be very often, but I'm not keen on find-and-replace approach when I have python behind me!
Your best option would probably use a tool like this: allbuttonspressed.com/projects/django-mediagenerator
I've never done it myself, but Django has a built in cache layer, see here: docs.djangoproject.com/en/1.4/topics/cache
0

I'm not sure this is good method or not. Probably you can add class to element to which you want specific style to be applied.

Anyway if you want to continue this method, you can implement your context processor which will add these variables by default. Refer Writing your context processor

1 Comment

thanks Rohan. The colours/styles I'm assigning in these variables are site-wide, and the only reason I assign them like this is to avoid having to find and replace them if I want to make a small tweak. Therefore, I think adding class to each element I want to style (everything!) would be more work for me. Also, I like the idea of writing context processors but it seems inefficient to pass the dict to every template by default when I only need it in one css view. What advantages can you see to using this approach that I'm not seeing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.