I am looking to implement a feature that allows users to select an "interface theme":
To enable users to change the theme across all templates, I have created a dropdown in my base.html.
For all my HTML templates, I utilize a base.html file by extending it using {% extends 'base.html'%}.
'# All views will require this:
theme = MyThemeModel.objects.filter(user=self.request.user).first().theme
return render(request, 'mytemplate.html', {'theme': theme})
'#base.html
{% if theme == 'theme 1'%}
<link rel="stylesheet" type="text/css" href="{% static 'theme1.css' %}" />
{% elif theme == 'theme 2'%}
<link rel="stylesheet" type="text/css" href="{% static 'theme2.css' %}" />
{% elif theme == 'theme 3'%}
<link rel="stylesheet" type="text/css" href="{% static 'theme3.css' %}" />
{% endif%}
I am considering utilizing AJAX to retrieve the selected theme from the user. However, I am concerned about having to add a function to each existing view. I aim to minimize code repetition. Is there a simpler way to achieve this?