Translatable version of django.contrib.flatpages with menu support.
- Django
- django_modeltrans
- django_mptt
- django-pragmatic
Tested with Django 1.8.
Install python library using pip:
pip install django-flatpages-i18n
Add
mptt
,modeltrans
andflatpages_i18n
toINSTALLED_APPS
in your Django settings fileMigrate your database
Specify desired languages in your Django settings file:
from django.utils.translation import gettext LANGUAGE_CODE = 'en' LANGUAGES = ( ('en', gettext('English')), ('de', gettext('German')), )
Addd
'flatpages_i18n.urls'
to your urls.py:if 'flatpages_i18n' in settings.INSTALLED_APPS: urlpatterns += i18n_patterns( path(pgettext_lazy('url', 'pages/'), include('flatpages_i18n.urls')), )
In your HTML template:
{% load i18n flatpages_i18n %} {% get_flatpages_i18n as flatpages_i18n %} <ul> {% for flatpage in flatpages_i18n %} <li><a href="{{ flatpage.get_absolute_url }}">{{ flatpage }}</a></li> {% endfor %} </ul>
To get flatpage by its PK:
{% get_flatpage_i18n 123 as my_flatpage %} {{ my_flatpage.content_i18n }}
or by its machine_name:
{% get_flatpage_i18n 'my-flatpage' as my_flatpage %}
To print all menu items:
<div id="navigation"> {% menu_i18n %} </div>
to get only children of menu item identified by its PK:
<div id="navigation"> {% menu_i18n 2 %} </div>
or by its machine_name:
<div id="navigation"> {% menu_i18n 'footer-menu' %} </div>
You can also assign menu items into variable:
{% get_menu_i18n 'my-menu' as my_menu %} {% for item in my_menu %} <a href="{{ item.get_absolute_url }}" target="{{ item.target }}">{{ item }}</a> {% endfor %}
Library is by Erik Telepovsky from Pragmatic Mates. See our other libraries.