Translated Fields not Rendering Correctly in Django CMS Plugin

29 Views Asked by At

Am encountering an issue with rendering translated fields in a Django CMS Plugin using django-modeltranslation. The plugin is supposed to display translated content based on the selected language, but it only renders with the default language.

All other non-CMS plugin parts on the page that also make use of django-modeltranslation translate and are displayed correctly.

Here's a brief overview of the setup:

  • I'm using django==4.2.10; django-cms==4.1.0; along with django-modeltranslation==0.18.11 for model field translations.
  • The plugin in question (HeaderPlugin) has a single field that is translated using django-modeltranslation.
  • The template (cms_plugins/header/list_items.html) only includes {{ instance }},
    but no value is displayed when non-default languages are selected. That is {{ instance }} only displays the instance when the default language is selected.
  • The Plugin is placed within a {% static_alias 'header' %}. A noteworthy observation is that, when I want to set the plugin for a non-default language page via the menu dropdown Aliases..., it redirects to the default language (since the page does not exist). Hence I am only able to set the plugin from the default language.

The plugin and associated model:

#cms_plugins.py
@plugin_pool.register_plugin
class HeaderPlugin(CMSPluginBase):
    model = HeaderPlugin
    module = _("Header")
    name = _("Header Plugin")
    render_template = "cms_plugins/header/list_items.html"

    def render(self, context, instance, placeholder):
        print("Not displayed when rendering in a non-default language")
        context.update({
            'instance': instance,
            'placeholder': placeholder,
            'lan_nl': instance.html_nl,
            'lan_en': instance.html_en,
        })
        return context

# models.py
class HeaderPlugin(CMSPlugin):
    header = models.ForeignKey(Header,
                               on_delete=models.PROTECT,
                               blank=False, null=False,
                               help_text=_("Select a header"))
    html = HTMLField(blank=True, default="",
                     help_text=_("Automatically generated field to reduce overhead"))

    def __str__(self):
        return f"Header plugin | {self.header.name}"

The template cms_plugins/header/list_items.html only contains {{ instance }}. The model (HeaderPlugin) is correctly configured for translation using django-modeltranslation, and the fields are registered for translation in the model's translation options. Translations exist for the HeaderPlugin instance. I've verified this using Django admin, the template {{ lan_nl }} and {{ lan_en }} (when the default language is selected).

Any suggestions or insights on how to troubleshoot and resolve this issue would be greatly appreciated.

0

There are 0 best solutions below