简体   繁体   English

我希望管理员用户从 Django 管理面板更改网站的字体、颜色和主题?

[英]I want Admin user to change the font, color and theme of a website from Django admin panel?

what is the best way to manage this?管理此问题的最佳方法是什么? Thank You in advance for reaching out and trying to help.提前感谢您伸出援手并提供帮助。

It can be easily done via context_processor , bit of logic and SingleInstanceMixin class:它可以通过context_processor 、逻辑位和SingleInstanceMixin class 轻松完成:

class SingleInstanceMixin(object):
    """Makes sure that no more than one instance of a given model is created."""

    def clean(self):  # noqa: D102
        model = self.__class__
        if model.objects.exists() and self.id != model.objects.get().id:
            raise ValidationError(
                _('Only one object of {0} can be created').format(model.__name__),
            )
        super().clean()

The mixin above you can paste into views.py or mixins.py file.您可以将上面的 mixin 粘贴到views.pymixins.py文件中。 Then, you create Settings model in models.py file:然后,在models.py文件中创建Settings model :

class Settings(SingleInstanceMixin, models.Model):
    font_name = models.CharField(_('Font name'), max_length=255)
    color = models.CharField(_('Color'), max_length=8)
    theme_name = models.CharField(_('Theme name'), max_length=64)

    def __str__(self):
        return _('Settings instance')

You can create also function that creates Settings object when it's needed and populates it with default values:您还可以创建 function 以在需要时创建设置 object 并使用默认值填充它:

def get_settings():
    return Settings.objects.get_or_create()[0]

(You must define default values in Settings model then). (您必须在设置 model 中定义默认值)。

After all, you pass Settings instance to context_processors.py毕竟,您将Settings实例传递给context_processors.py

def website(request):
    return {
        'settings': get_settings(),
    }

Rest of logic you need to write yourself, in views, templates etc. But the initial idea how todo that is shown in code I posted here. Rest 您需要在视图、模板等中编写自己的逻辑。但是我在此处发布的代码中显示了如何执行此操作的初步想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM