parler.views module¶
The views provide high-level utilities to integrate translation support into other projects.
The following mixins are available:
ViewUrlMixin
- provide aget_view_url
for the {% get_translated_url %} template tag.TranslatableSlugMixin
- enrich theDetailView
to support translatable slugs.LanguageChoiceMixin
- add?language=xx
support to a view (e.g. for editing).TranslatableModelFormMixin
- add support for translatable forms, e.g. for creating/updating objects.
The following views are available:
TranslatableCreateView
- TheCreateView
withTranslatableModelFormMixin
support.TranslatableUpdateView
- TheUpdateView
withTranslatableModelFormMixin
support.
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 theview_url_name
together withview.args
andview.kwargs
construct the URL. When some arguments are translated (e.g. a slug), theget_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})
-
get_view_url
()¶ 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 URLargs
andkwargs
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.
Example:
class ArticleDetailView(TranslatableSlugMixin, DetailView): model = Article template_name = 'article/details.html'
-
get_language
()¶ Define the language of the current view, defaults to the active language.
-
get_language_choices
()¶ Define the language choices for the view, defaults to the defined settings.
-
get_object
(queryset=None)¶ Fetch the object using a translated slug.
-
get_translated_filters
(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.-
get_current_language
()¶ Return the current language for the currently displayed object fields. This reads
self.object.get_current_language()
and falls back toget_language()
.
-
get_default_language
(object=None)¶ Return the default language to use, if no language parameter is given. By default, it uses the default parler-language.
-
get_language
()¶ Get the language parameter from the current request.
-
get_language_tabs
()¶ Determine the language tabs to show.
-
get_object
(queryset=None)¶ 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): pass
-
get_form_class
()¶ Return a
TranslatableModelForm
by default if no form_class is set.
-
get_form_kwargs
()¶ 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’sCreateView
.
The TranslatableUpdateView
class¶
-
class
parler.views.
TranslatableUpdateView
(**kwargs)¶ Update view that supports translated models. This is a mix of the
TranslatableModelFormMixin
and Django’sUpdateView
.