Evgenii Legotckoi
Nov. 22, 2019, 1:03 p.m.

Django - Tutorial 050. Creating Dynamic Site Settings Using SingletonModel

Let's say you create a site with CMS based on Django, which must have some kind of dynamic site settings that will be available to the user. For example, the name of the site, some specialized information, while you take into account the possibility of multilingualism. What then can be used for this? I got the idea to use a database.

To implement this, the following is required:

  1. Creating a data model that will always contain only one object, that is, only one record. That is, it will be a Singleton Model.
  2. Prohibit deleting this entry and creating new ones in the Django admin panel
  3. The ability to use information from this model directly in the template, without loading the site settings in the view function.

Let's figure out in order how to implement this.


SingletonModel

I found on GitHub the code of the abstract SingletonModel, which was written back in 2012. Here is a link to gist .

Here is the code for this SingletonModel

  1. class SingletonModel(models.Model):
  2. class Meta:
  3. abstract = True
  4.  
  5. def save(self, *args, **kwargs):
  6. self.__class__.objects.exclude(id=self.id).delete()
  7. super(SingletonModel, self).save(*args, **kwargs)
  8.  
  9. @classmethod
  10. def load(cls):
  11. try:
  12. return cls.objects.get()
  13. except cls.DoesNotExist:
  14. return cls()

The model in the save method automatically saves all the others when saving the object, which allows you to always keep only one instance of this model in the database.

The load method takes from the database the only settings object, if the object does not exist in the database, it returns a new instance of this model, which will need to be saved later.

Let's create a custom settings class to use it for our needs in the future.

  1. # -*- coding: utf-8 -*-
  2.  
  3. from django.db import models
  4. from django.utils.translation import ugettext_lazy as _
  5.  
  6. from my_app.singleton_model import SingletonModel
  7.  
  8.  
  9. class SiteSettings(SingletonModel):
  10. site_url = models.URLField(verbose_name=_('Website url'), max_length=256)
  11. title = models.CharField(verbose_name=_('Title'), max_length=256)
  12.  
  13. def __str__(self):
  14. return 'Configuration'

Setting up the administration panel

Now you need to prohibit deleting this entry and creating new ones in the Django admin panel.

  1. # -*- coding: utf-8 -*-
  2.  
  3. from django.contrib import admin
  4. from django.db.utils import ProgrammingError
  5.  
  6. from my_app.models import SiteSettings
  7.  
  8.  
  9. class SiteSettingsAdmin(admin.ModelAdmin):
  10. # Create a default object on the first page of SiteSettingsAdmin with a list of settings
  11. def __init__(self, model, admin_site):
  12. super().__init__(model, admin_site)
  13. # be sure to wrap the loading and saving SiteSettings in a try catch,
  14.         # so that you can create database migrations
  15. try:
  16. SiteSettings.load().save()
  17. except ProgrammingError:
  18. pass
  19.  
  20. # prohibit adding new settings
  21. def has_add_permission(self, request, obj=None):
  22. return False
  23.  
  24. # as well as deleting existing
  25. def has_delete_permission(self, request, obj=None):
  26. return False
  27.  
  28.  
  29. admin.site.register(SiteSettings, SiteSettingsAdmin)
  30.  

Now the settings can be used in Python code

Creating a context processor to load settings into a template

context_proccessors.py

  1. # -*- coding: utf-8 -*-
  2.  
  3. from evileg_settings.models import SiteSettings
  4.  
  5.  
  6. def load_settings(request):
  7. return {'site_settings': SiteSettings.load()}
  8.  

settings.py

Then we connect load_settings

  1. TEMPLATES = [
  2. {
  3. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  4. 'DIRS': [],
  5. 'APP_DIRS': True,
  6. 'OPTIONS': {
  7. 'context_processors': [
  8. 'django.template.context_processors.debug',
  9. 'django.template.context_processors.request',
  10. 'django.contrib.auth.context_processors.auth',
  11. 'django.contrib.messages.context_processors.messages',
  12. # Загружаем наши настройки при каждом рендеринге шаблона с контекстом
  13. 'my_app.context_processors.load_settings',
  14. ],
  15. },
  16. },
  17. ]

Usage in the template

  1. <h1>{{ site_settings.title }}</h1>

Conclusion

So quickly and simply, you can implement simple dynamic site settings in Django and then bind them to all the site parameters that interest us.
The advantage of this approach will be the ability to use foreign keys for some special data on the site, as well as the use of django modeltranslation, which means there will be no problems with multilingualism.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html