Reputation: 1182
Is it possible to make part of a page use a totally different CSS file when the user chooses a different option in a select box using Django?
What I'm basically trying to do is add a theme selector to the page.
Thanks.
Upvotes: 1
Views: 323
Reputation: 11248
To create a theme selector in Django use a form to get the stylesheet choice from the user and store the style choice in a session. The valid form should execute:
request.session['style'] = the_style_choice
https://docs.djangoproject.com/en/1.5/topics/http/sessions/
Use a context processor to return the right stylesheet each request in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"MyContextProcessor.get_style",
)
In MyContextProcessor.py
def get_style(request):
if 'style' in request.session:
style = request.session.get('style')
else:
style = 'some_style.css'
request.session['style'] = style
return {'style': style, }
https://docs.djangoproject.com/en/1.5/ref/templates/api/#subclassing-context-requestcontext
In your base template add the style:
{{ style }}
You can use Ajax in the form to give the user a smooth experience: https://docs.djangoproject.com/en/1.5/topics/class-based-views/generic-editing/#ajax-example
Upvotes: 2
Reputation:
This should be done with javascript, not Django. Look into doing it with Jquery.
Edit: How do I switch my CSS stylesheet using jQuery?
Upvotes: -1