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',
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?
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.
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.
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.
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.
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.
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.
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.
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.