diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 7af2ba420..e098d8ba2 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -11,11 +11,12 @@ * Trent Holiday (trumpet2012) * Tom Dyson (tomdyson) * Oliver Bestwalter (obestwalter) +* Nguyễn Hồng Quân (hongquan) ## Translators -* François GUÉRIN (frague59) +* François GUÉRIN (frague59) * Claudio Marinozzi (pyMan) * Pierre Geier (bloodywing) * Max Kurama (MaxKurama) diff --git a/wagtailmenus/models/menus.py b/wagtailmenus/models/menus.py index d491a8ab6..e2de8d267 100644 --- a/wagtailmenus/models/menus.py +++ b/wagtailmenus/models/menus.py @@ -6,7 +6,7 @@ from django.db import models from django.core.exceptions import ImproperlyConfigured, ValidationError -from django.template import Context +from django.template.loader import get_template, select_template from django.utils import six from django.utils.encoding import python_2_unicode_compatible from django.utils.functional import cached_property, lazy @@ -186,8 +186,8 @@ def render_to_template(self): """ context_data = self.get_context_data() template = self.get_template() - context_data['current_template'] = template.name - return template.render(Context(context_data)) + context_data['current_template'] = template.template.name + return template.render(context_data) def clear_page_cache(self): try: @@ -303,7 +303,11 @@ def get_context_data(self, **kwargs): the 'sub_menu' tag to render any additional levels.""" ctx_vals = self._contextual_vals opt_vals = self._option_vals - data = ctx_vals.parent_context.flatten() + try: + data = ctx_vals.parent_context.flatten() + except AttributeError: + # Jinja2 Context + data = ctx_vals.parent_context.get_all() data.update(ctx_vals._asdict()) data.update({ 'apply_active_classes': opt_vals.apply_active_classes, @@ -511,19 +515,19 @@ def modify_menu_items(self, menu_items): """ return menu_items - def get_template_engine(self): - return self._contextual_vals.parent_context.template.engine - def get_template(self): - engine = self.get_template_engine() + """ + In Django template backend case, returns the + ``django.template.backends.django.Template`` instance. + """ specified = self._option_vals.template_name if specified: - return engine.get_template(specified) + return get_template(specified) if self.template_name: # Developers can set 'template_name' as a class attribute to have # custom menus use specific templates - return engine.get_template(self.template_name) - return engine.select_template(self.get_template_names()) + return get_template(self.template_name) + return select_template(self.get_template_names()) def get_template_names(self): """Return a list (or tuple) of template names to search for when diff --git a/wagtailmenus/models/mixins.py b/wagtailmenus/models/mixins.py index c60071bb6..67c64ac40 100644 --- a/wagtailmenus/models/mixins.py +++ b/wagtailmenus/models/mixins.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, unicode_literals from django.utils.functional import cached_property +from django.template.loader import get_template, select_template from .. import app_settings @@ -9,13 +10,12 @@ class DefinesSubMenuTemplatesMixin(object): sub_menu_template_name = None # set to use a specific default template def get_sub_menu_template(self): - engine = self.get_template_engine() specified = self._option_vals.sub_menu_template_name if specified: - return engine.get_template(specified) + return get_template(specified) if self.sub_menu_template_name: - return engine.get_template(self.sub_menu_template_name) - return engine.select_template(self.get_sub_menu_template_names()) + return get_template(self.sub_menu_template_name) + return select_template(self.get_sub_menu_template_names()) @cached_property def sub_menu_template(self): @@ -52,7 +52,7 @@ def get_context_data(self, **kwargs): """ data = {} if self._contextual_vals.current_level == 1 and self.max_levels > 1: - data['sub_menu_template'] = self.sub_menu_template.name + data['sub_menu_template'] = self.sub_menu_template.template.name data.update(kwargs) return super(DefinesSubMenuTemplatesMixin, self).get_context_data( **data)