[Answered ]-Swich to light or dark mode on Django?

1👍

In very basic terms you would just change the CSS file depending on whether dark mode is selected or not:

{% if request.user.darkmode %}
<link rel="stylesheet" href="{% static 'css/dark.css' %}">
{% else %}
<link rel="stylesheet" href="{% static 'css/light.css' %}">
{% endif %

You can track darkmode in the session like so:

if request.POST.get('darkmode'):
   request.session['darkmode'] = True

You would have to send darkmode to the view in a POST request. Because you aren’t using javascript, you would likely need a view just for setting darkmode and redirecting to another view.

The other option is to add darkmode as a field to the User model so that it’s always saved to the current user once it has been set.

👤0sVoid

Leave a comment