def get_section_context(request, path): path = path.split('/') section_count = len(path) if section_count > 31: raise Http404 flt = {} while path: flt['parent__' * len(flt) + 'slug'] = path.pop() flt['parent__' * (len(flt)-1) + 'language'] = request.LANGUAGE_CODE if section_count == 1: flt['parent__isnull'] = True try: item = Section.objects.get(**flt) except Section.DoesNotExist: raise Http404 if item.is_root_node(): root = item ancestors = None siblings = None else: ancestors = item.get_ancestors() root = ancestors[0] siblings = item.get_siblings(include_self=True) submenu = root.get_children() children = submenu if item is root else item.get_children() return {'item': item, 'ancestors': ancestors, 'root': root, 'submenu': submenu, 'children': children, 'siblings': siblings}