The task is to manage the content (for example, the inscription in the header) through the Django admin panel. + should be the possibility of multilingualism.

I already understood how to manage models, but how to create a form for managing content.

I moved the whole Internet, I did not find anything. Bail out, colleagues!

  • Create a model with the settings, obviously the same) - andreymal
  • Thanks, @andreymal! Could you please give me a materialchik? - Roman Denisenko
  • By the way, do your usual i18n and gettext tasks just don’t fit? - andreymal
  • I understand that in this case, the text is taken from the localization files, and I need to manage this through the admin panel, and not edit the files :) - Roman Denisenko
  • You can still try django-cms, it knows how out of the box. But it is configured very nontrivially, not for beginners, I would say - FeroxTL

1 answer 1

I do something like this in my projects.

Step zero: we are determined with the list of supported languages

If you don’t need to support Arabic, Japanese and other exotic things, you can set up in the settings a list of languages ​​that will work on the site:

# settings.py from django.utils.translation import ugettext_lazy as _ LANGUAGES = [ ('en', _('English')), ('ru', _('Russian')), ] 

By default, all languages ​​available in Django are set up in the LANGUAGES setting, so if you don’t want to restrict anything, you can leave the settings unchanged.

Step one: create a model for storing settings

 # models.py from django.conf import settings from django.utils.translation import ugettext_lazy as _ class WebsiteSettings(models.Model): language = models.CharField(max_length=16, unique=True, choices=settings.LANGUAGES, verbose_name=_('Language')) some_text = models.TextField(blank=True, verbose_name=_('Some text')) class Meta: verbose_name = _('website settings') verbose_name_plural = _('website settings') ordering = ['language'] def __str__(self): return dict(settings.LANGUAGES).get(self.language, self.language) 

All salt in the language field: each language has its own and unique copy of WebsiteSettings . All other fields are registered to your taste as needed. Migrations, admin and other rubbish are done in the same way as for any other model.

Step two: we push settings in templates

The cap is usually on every page, but each time it’s inconvenient to get the settings in each view, so we will register the receiving of the settings in context_processors .

Create such a context_processors.py file (or context_processors.py existing one):

 from .models import WebsiteSettings def website_settings(request): web_settings = WebsiteSettings.objects.filter(language=request.LANGUAGE_CODE).first() if not web_settings: web_settings = WebsiteSettings(language='fallback') return {'web_settings': web_settings} 

And turn it on in the settings:

 # settings.py TEMPLATES = [ { # ... все Š¾ŃŃ‚Š°Š»ŃŒŠ½Ń‹Šµ параметры ... 'OPTIONS': { 'context_processors': [ # ... все Š¾ŃŃ‚Š°Š»ŃŒŠ½Ń‹Šµ параметры ... 'ŠøŠ¼ŃŠæŃ€ŠøŠ»Š¾Š¶ŠµŠ½ŠøŃ.context_processors.website_settings', ], }, }, ] 

Now in each template there will be a web_settings object for the current language, the fields of which can be accessed like this:

 {{ web_settings.some_text }} 

As Django selects the current language, read somewhere in the documentation . And LocaleMiddleware not forget to include LocaleMiddleware .

  • Thank you for such a detailed description. It really helped me. But there is one drawback. After all, I can add as many records as I like for the Russian language. Then it will not be completely clear which of them will be displayed on the page. I think it would be better to do something like this: Section "Cap"> tabs (groups) "En", "Ru" ...> Title, subtitle .... But how ??) - Roman Denisenko
  • ā€œI can’t understand as many records as I like for the Russian languageā€, it’s unique=True and the admin’s settings will not allow you to create several settings for the Russian language. If you want not to interfere with all the settings in a bunch, then nothing prevents you from creating by analogy models like ā€œShapkaSettingsā€, ā€œPodvalSettingsā€, and so on) - andreymal
  • pancake. For sure! You are God)) Tell me finally, are there any other ways to do this? In which direction should I google? It’s just for self-development, as I acquainted with django - Roman Denisenko
  • I don’t know, and I’m not quite looking for me, and this is fine.) One can only suggest storing all the settings in a single json-field of a single model, and in the admin panel draw a beautiful widget with all sorts of sections to control this json-field, but for django familiar recently "it will be difficult - andreymal
  • Good. And if I want to add languages, too, through the admin area. How can I do it? I've created a model for languages, but now I need to substitute instead of choices=settings.LANGUAGES - Roman Denisenko