Dark and light theme implementation

I tried to create a dark-light theme toggle and store option for my Django project…… try and fail atm.

Disclaimers :blush:

  • I am learning Django for 6 months partime
  • Sorry for the typo’s….I dont have proper handfunction

My current implementation is based upon this page

I created the following:
Model:

from django.db import models
from django.conf import settings

from django.contrib.auth.models import User
from django.utils.translation import gettext, gettext_lazy as _

class ThemeConfiguration(models.Model):
    THEME = [
        (True, _('dark')),
        (False, _('light')),
    ] 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    theme = models.BooleanField(_('theme'), default=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['user'], name='One Entry Per User')
        ]

In same app; context_processors.py:

from .models import ThemeConfiguration

def theme(request):
    if request.user.is_authenticated:
        _theme = ThemeConfiguration.objects.filter(user=request.user).last()
    else:
        _theme = None
    return {
        'theme': _theme,
    }

**In settings.py templates I added **
'MyConfiguration.context_processors.theme',

In my main template I added

{% if theme.theme == 'dark' %}
    <link href="{% static 'theme/css/themes/layout/header/base/dark.css' %}" rel="stylesheet" type="text/css" />
{% else %}
    <link href="{% static 'theme/css/themes/layout/header/base/light.css' %}" rel="stylesheet" type="text/css" />
{% endif %}
And now I am lost…..So my questions are:
1.	What should I do to get the right theme in the main template working. The following code is wrong but I don’t know how to solve it
2.	How can I create a toggle button for it that stores the requested data in the model?
    -	I realize I have to prevent multiple instances of the themeConfiguration for the same user but don’t know how and where. 
3.	How can I check the prefers-color-scheme so I can use that if needed? 
    - and change the admin color scheme based on the user preferences

Not totally on the topic…. Is it appreciated if I create a small project with this functioning and place it in the show & tell part of the forum?

Hi FireDragon42.

  1. What should I do to get the right theme in the main template working. The following code is wrong but I don’t know how to solve it

What is wrong about it? I’m confused as to what you’re looking for from us.

  1. How can I create a toggle button for it that stores the requested data in the model?
    - I realize I have to prevent multiple instances of the themeConfiguration for the same user but don’t know how and where.

I would create an ajax function based view that would toggle (or select depending on options available) the setting. Regarding the actual DB update, take a look at update_or_create. Regarding preventing multiple instances, you could consider using a OneToOneField rather than ForeignKey for ThemeConfiguration.user.

  1. How can I check the prefers-color-scheme so I can use that if needed?
    - and change the admin color scheme based on the user preferences

You’ll need to decide what configuration takes precendence? Does prefers-color-scheme always win? Or does a selected configuration win and until the user makes a choice it falls back to prefers-color-scheme. Once you have your answer there, then you can enforce that in your css.

Regarding the django admin color scheme, the docs give some information on how to handle that. If you have further questions, let us know.

Is it appreciated if I create a small project with this functioning and place it in the show & tell part of the forum?

I think there’s value in this work, even if it’s simply what you’ve learned about Django. If you have some expectations of the community, I’d suggest making them known when you post it. For example, if you want people to poke around the site and look for bugs, or review the code or anything else you may be looking for.

CodenameTim thank you for the reply

the code in the main template : {% if theme.theme == 'dark' %} etc is not working. It always adds the light.css and I dont know why. Beside that I am doing something wrong in the code to refer to the theme.

I will try that. OneToOneField and ajax function based view. I never found the update_or_create() Thanks.

I will try but might need some help later. Learning everything atm. I will overrule prefers-color-scheme if the user set a theme. I will try to implement that.

I am learning everything atm and some things are hard to find. I will try to make a small simple project and upload that based upon your answers.

Thanks for the response. Mutch appreciated.

Is the if statement evaluating to true? If you can’t tell, you can render out the value directly into the template {{ theme.theme }} or install the django debug toolbar.

Sounds good!

I may have misunderstood what you were asking earlier then. I thought you were wondering if you could/should show off your project when you’re done. I don’t think it’s necessary yet for you to upload a MVP of this to debug your issues.

Note that theme here is a Boolean. It’s going to be True or False - it’s never going to be 'dark'.

Thank you KenWhitesell
I wonder what happen if I check the solution button here too :slight_smile:

{% if theme.theme == True %}

works. Thanks

@ FireDragon42

Also keep in mind that the True only exists on the django level. It just updates the field to True when you write from code. It doesn’t actually set a DEFAULT Constraint on the DB end. If you want that, you need to do that separately.

Hi,

I am wondering if you were able to make dark mode on your site? If so, could you upload the working code? I am trying to make a dark mode button, but are having several issues with the code.

If you’re having a problem with your implementation, I’d suggest you open a new topic with your code that you’re trying to use. It’s likely a different problem with a different solution than the issue discussed here.

I will post it soon. Sorry the mail went into the spam box so I am late

1 Like

Do you know another forum where someone can answer my problem with Missing user in REST serializer ? Thanks Fire

That appears to be a Django REST Framework question - their support channels are documented under “Support”

thanks thanks thanks… 3 times to get 20 characters for a post :slight_smile:
Fire