0

Im writting an app on django, and want to implement a feature that change the CSS settings. For example, on my settings module i want to change the color of the font, or the size of the font, any idea of how can i implement that?.

Thanks!

2
  • 3
    Your question is very unclear.. Are you talking about an interface, or something which changes the look and feel of the website ? Also, Have you looked into docs.djangoproject.com/en/1.7/howto/static-files/… if that is what you are asking for ? Commented Jan 19, 2015 at 17:19
  • Sorry if my question wasn't very clear, but all the answers below match what i'm looking for. Thanks for the advice ;). Commented Jan 19, 2015 at 17:37

4 Answers 4

1

I'm using a custom view to return a dynamic CSS file; it uses file caching to avoid rendering over and over for each request.

View function:

@cache_page(None)  # won't expire, ever
def dyn_css(request):
    (css_theme, created) = CSS_Theme.objects.get_or_create(pk=1)
    return render_to_response('dynamic.css', {'theme': css_theme},
                              content_type="text/css")

Reset function, called "manually" when needed:

def reset_cache(request, view='dyn_css', args=None):
    from django.core.cache import cache
    from django.utils.cache import get_cache_key

    if args is None:
        path = reverse(view)
    else:
        path = reverse(view, args=args)

    request.path = path
    key = get_cache_key(request)
    if key in cache:
        cache.delete(key)

CSS file:

.row {
    background-color: {{ theme.background_color }} !important;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have it in my base.html file like so: <link rel="stylesheet" type="text/css" href="/dynamic_css/global.css">
1

The simplest way would be to create a variable in your settings.py file called FONT_COLOR (for example).

Then, when you are rendering a template, you could add FONT_COLOR to the response context and use it to override your CSS.

template.html

...

<style>
    body {
        color: {{ FONT_COLOR }};
    }
</style>

...

Comments

1

There are a few different approaches you can take here.

The easiest is to have static css for classes that you set dynamically.:

<P class='{{ dynamic_classes }}'>

You could also make some CSS overrides in your regular views. For example, if you wanted a to render in red under a "warning" condition:

</head>
<style>
   {% if warning %}
   b {color: red;}
   {% endif %}
</style>

It is also possible to render your CSS as a dynamic view instead of static, but I'd explore both of these other options first.

Comments

0

You should check http://sass-lang.com/ It allows you to set variables such as colors and sizes, then use theme across your website or app, and when you need to change something all you have to do is change the variable.

Comments

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.