parler.views module

The views provide high-level utilities to integrate translation support into other projects.

The following mixins are available:

The following views are available:

The ViewUrlMixin class

class parler.views.ViewUrlMixin

Provide a view.get_view_url method in the template.

This tells the template what the exact canonical URL should be of a view. The {% get_translated_url %} template tag uses this to find the proper translated URL of the current page.

Typically, setting the view_url_name just works:

class ArticleListView(ViewUrlMixin, ListView):
    view_url_name = 'article:list'

The get_view_url() will use the view_url_name together with view.args and view.kwargs construct the URL. When some arguments are translated (e.g. a slug), the get_view_url() can be overwritten to generate the proper URL:

from parler.views import ViewUrlMixin, TranslatableUpdateView
from parler.utils.context import switch_language

class ArticleEditView(ViewUrlMixin, TranslatableUpdateView):
    view_url_name = 'article:edit'

    def get_view_url(self):
        with switch_language(self.object, get_language()):
            return reverse(self.view_url_name, kwargs={'slug': self.object.slug})

This method is used by the get_translated_url template tag.

By default, it uses the view_url_name to generate an URL. When the URL args and kwargs are translatable, override this function instead to generate the proper URL.

view_url_name = None

The default view name used by get_view_url(), which should correspond with the view name in the URLConf.

The TranslatableSlugMixin class

class parler.views.TranslatableSlugMixin

An enhancement for the DetailView to deal with translated slugs. This view makes sure that:

  • The object is fetched in the proper translation.
  • The slug field is read from the translation model, instead of the shared model.
  • Fallback languages are handled.
  • Objects are not accidentally displayed in their fallback slug, but redirect to the translated slug.


class ArticleDetailView(TranslatableSlugMixin, DetailView):
    model = Article
    template_name = 'article/details.html'

Define the language of the current view, defaults to the active language.


Define the language choices for the view, defaults to the defined settings.


Fetch the object using a translated slug.


Allow passing other filters for translated fields.

The LanguageChoiceMixin class

class parler.views.LanguageChoiceMixin

Mixin to add language selection support to class based views, particularly create and update views. It adds support for the ?language=.. parameter in the query string, and tabs in the context.


Return the current language for the currently displayed object fields. This reads self.object.get_current_language() and falls back to get_language().


Return the default language to use, if no language parameter is given. By default, it uses the default parler-language.


Get the language parameter from the current request.


Determine the language tabs to show.


Assign the language for the retrieved object.

The TranslatableModelFormMixin class

class parler.views.TranslatableModelFormMixin

Mixin to add translation support to class based views.

For example, adding translation support to django-oscar:

from oscar.apps.dashboard.catalogue import views as oscar_views
from parler.views import TranslatableModelFormMixin

class ProductCreateUpdateView(TranslatableModelFormMixin, oscar_views.ProductCreateUpdateView):

Return a TranslatableModelForm by default if no form_class is set.


Pass the current language to the form.

The TranslatableCreateView class

class parler.views.TranslatableCreateView(**kwargs)

Create view that supports translated models. This is a mix of the TranslatableModelFormMixin and Django’s CreateView.

The TranslatableUpdateView class

class parler.views.TranslatableUpdateView(**kwargs)

Update view that supports translated models. This is a mix of the TranslatableModelFormMixin and Django’s UpdateView.