Welcome to AutoBreadcrumbs’s documentation!

This is a Django application to automatically build breadcrumbs like this :

Home > Some page > Some child page

Each crumb displays a title with a link to represent a view, crumb tree is determined from the urls map of your project, crumbs are linked to view url name (including namespaces).

Dependancies

User’s Guide

Install

pip install autobreadcrumbs
  1. In your settings file add autobreadcrumbs to your installed apps:

    INSTALLED_APPS = (
        ...
        'autobreadcrumbs',
        ...
    )
    
  2. Import default settings:

    from autobreadcrumbs.settings import *
    
  3. Register its context processor:

    TEMPLATE_CONTEXT_PROCESSORS = (
        ...
        'autobreadcrumbs.context_processors.AutoBreadcrumbsContext',
        ...
    )
    

    (Order with other context processors don’t matter).

  4. Finally add these two lines in your main urls.py:

    import autobreadcrumbs
    autobreadcrumbs.autodiscover()
    

    This is optional but if you don’t do this, all crumbs.py file will be ignored and only titles defined in settings.AUTOBREADCRUMBS_TITLES will be used.

Overview

Autobreadcrumbs build breadcrumbs for a view using its url to find other views the url goes through.

Crumb principle is to link a crumb title to an url name. You can’t add a crumb that is not related to an url name.

There are three way to define crumbs:

  • In the initial crumb dictionnary settings.AUTOBREADCRUMBS_TITLES;
  • At project level in a crumb module that will be loaded from defined Python path in settings.AUTOBREADCRUMBS_ROOT_CRUMB;
  • Per application with a crumb module in your application enabled from settings.INSTALLED_APPS;

The first way is a simple dictionnary to populate. The other ways use Python modules that will directly manage crumbs using the registry interface.

When crumbs are defined, they need to be discovered so they can be loaded to populate registry.

Finally the template context processor is in charge to resolve breadcrumbs for current ressource (a view instance) and push resolved datas in template context.

Example

With following main urls.py:

from django.conf.urls import include, url
from django.views.generic.base import TemplateView

urlpatterns = [
    url(r'^$', TemplateView.as_view(
        template_name="page.html"
    ), name='home'),

    url(r'^foo/$', TemplateView.as_view(
        template_name="page.html"
    ), name='foo'),

    url(r'^foo/bar/$', TemplateView.as_view(
        template_name="page.html"
    ), name='bar'),
]

Following settings:

AUTOBREADCRUMBS_TITLES = {
    'home': 'Home',
}
# Assume your django project is a 'project/' directory
AUTOBREADCRUMBS_ROOT_CRUMB = 'project.crumbs'

And following crumbs.py at project root:

from autobreadcrumbs.registry import breadcrumbs_registry
from django.utils.translation import ugettext_lazy

breadcrumbs_registry.update({
    'foo': 'Foo',
    'bar': ugettext_lazy("Bar"),
})

The view at /foo/bar/ will resolve current ressource is passing through url names home, foo and finally bar.

This will lead to breadcrumbs:

Home > Foo > Bar

You can see usage of ugettext_lazy that can be used to store translation string for your titles if needed.

Settings

Template string for crumb item in tag {% autobreadcrumbs_links %}

autobreadcrumbs.settings.AUTOBREADCRUMBS_HTML_SEPARATOR = u' > '

Template string for separator item in tag {% autobreadcrumbs_links %}

autobreadcrumbs.settings.AUTOBREADCRUMBS_ROOT_CRUMB = None

Optional project level crumbs file, this have to be a correct Python path to a crumb module.

autobreadcrumbs.settings.AUTOBREADCRUMBS_TITLES = {}

Initial project crumbs where you can add initial crumbs for your project views.

Template tags

Every template tag render crumb title using template syntax with given context (from your view), so you can use something like {{ myvar }} in a title for a view which have the myvar variable available in its context. This means template blocks and filters can be used also.

Note

Template tags require some variable inside Template context that are injected from context processor autobreadcrumbs.context_processors.AutoBreadcrumbsContext. So you either have to enabled it in your template context processors or inject them from your views.

autobreadcrumbs.templatetags.autobreadcrumb.currentwalkthroughto(parser, token)[source]

Template tag to output content if current ressource walk through given url name.

Example

Template tag take one requirement argument name that is an url name to search for in breadcrumbs, if the current ressource crumb walk through it, content inside tag will be rendered:

{% load autobreadcrumb %}
{% currentwalkthroughto 'bar' %}Hello{% endcurrentwalkthroughto %}
autobreadcrumbs.templatetags.autobreadcrumb.current_title_from_breadcrumbs(context)[source]

Template tag to output breadcrumb title from current ressource crumb.

Example

{% load autobreadcrumb %}
{% current_title_from_breadcrumbs %}
autobreadcrumbs.templatetags.autobreadcrumb.autobreadcrumbs_tag(context)[source]

Template tag to output HTML for full breadcrumbs using template autobreadcrumbs_tag.html.

Example

{% load autobreadcrumb %}
{% autobreadcrumbs_tag %}

Template tag which use AUTOBREADCRUMBS_HTML_LINK and AUTOBREADCRUMBS_HTML_SEPARATOR settings to build a HTML list of breadcrumbs.

Returned HTML is marked as safe (not to escape) for templates.

Example

{% load autobreadcrumb %}
{% autobreadcrumbs_links %}

Developer’s Guide

Site registry for breadcrumb definitions

class autobreadcrumbs.registry.BreadcrumbSite(*args, **kwargs)[source]

Breadcrumbs site registry

Keyword Arguments:
 initial (dict) – Optional initial dictionnary of crumbs urlname->value. Default to an empty dict.
get_names()[source]

Return registred crumb url names.

Returns:List of registred crumb url names, sorted with default sorted() behavior.
Return type:list
get_registry()[source]

Return current registry

Returns:Currrent registry.
Return type:dict
get_title(name)[source]

Get title for given url name.

Parameters:name (string) – Url name.
Returns:Crumb title.
Return type:string or tuple
has_title(name)[source]

Find if given name is registred as a crumb.

Returns:True if name exists in current registry, else False.
Return type:bool
register(name, value)[source]

Register a crumb for given url name.

Parameters:
  • name (string) – Url name.
  • value (string or tuple) – Crumb title to define.
Raises:
  • AlreadyRegistered if the url name is allready registered in
  • crumbs.
reset()[source]

Reset registry to an empty Dict.

unregister(name)[source]

Unregister a crumb.

Parameters:name (string) – Url name.
Raises:NotRegistered if given url name is not registred yet.
update(crumbs)[source]

Update many crumbs

This works like the Dict.update({..}) method.

Parameters:crumbs (dict) – A dict of crumbs (urlname->value).
autobreadcrumbs.registry.breadcrumbs_registry = <autobreadcrumbs.registry.BreadcrumbSite object>

Default breadcrumbs site registry for a Django instance.

Crumb definitions discovering

Crumb definitions are registred through files that are loaded as Python modules, where some code can register needed crumbs.

autobreadcrumbs.discover.autodiscover(filename='crumbs')[source]

Automatic discovering for available crumbs definitions

Before looking at crumbs files, registry start from settings.AUTOBREADCRUMBS_TITLES items if setted, else an empty Dict.

Then it try to load possible root crumb file defined in settings.AUTOBREADCRUMBS_ROOT_CRUMB (as a Python path). And finally load each crumbs file finded in settings.INSTALLED_APPS.

Keyword Arguments:
 filename (string) – Module filename to search for. Default to crumbs, so it will search for a crumbs.py file at root of every enabled module from settings.INSTALLED_APPS.
Returns:List of successfully loaded Python paths.
Return type:list
autobreadcrumbs.discover.discover(module_path, filename=None)[source]

Try to discover and load a crumbs.py file from given Python path.

Parameters:module_path (string) – Python path to scan for filename module.
Keyword Arguments:
 filename (string) – Optional module filename to search for, usually crumbs.py, default to None.
Raises:Exception – Raise any occuring exception from loaded Python path.
Returns:Python path (module.filename) for discovered crumbs module. If filename does not exists in module, return None.
Return type:string or None

Template context processor

autobreadcrumbs.context_processors.AutoBreadcrumbsContext(request)[source]

Context processor to resolve breadcrumbs from current ressource.

Use request.path to know the current ressource url path and settings.ROOT_URLCONF to resolve it.

Development

Development requirement

autobreadcrumbs is developed with:

  • Test Development Driven (TDD) using Pytest;
  • Respecting flake and pip8 rules using Flake8;
  • Sphinx for documentation with enabled Napoleon extension (using only the Google style);
  • tox to test again different Python and Django versions;

Every requirement is available in file dev_requirements.txt (except for tox).

Install for development

First ensure you have pip and virtualenv installed, then in your console terminal type this:

mkdir autobreadcrumbs-dev
cd autobreadcrumbs-dev
virtualenv .
source bin/activate
pip install -r https://raw.githubusercontent.com/sveetch/autobreadcrumbs/master/requirements/dev.txt

autobreadcrumbs will be installed in editable mode from the last commit on master branch.

Unittests

Unittests are made to works on Pytest, a shortcut in Makefile is available to start them on your current development install:

make tests
Tox

To ease development again multiple Python and Django versions, a tox configuration has been added. You are strongly encouraged to use it to test your pull requests.

Before using it you will need to install tox, it is recommended to install it at your system level so dependancy is not in tests requirements file:

sudo pip install tox

Then go in the autobreadcrumbs module directory, where live the setup.py and tox.ini files and execute tox:

tox
Documentation

You should see about sphinx-autobuild for a watcher which automatically rebuild HTML documentation when you change sources.

Changelog

Version 2.0.0 - 2016/08/18

This is a major refactoring to adopt Test Driven Development, cleaner behaviors and better core API.

  • Added Unittests with Py.test;
  • Added tox configuration;
  • Added documentation;
  • Moved discovering from context_processors.py to discover.py;
  • Moved regitry stuff from __init__.py to registry.py;
  • Better docstring for code;
  • Many minor changes to follow Flake8;
  • Added AUTOBREADCRUMBS_ROOT_CRUMB setting to be able to use a crumbs.py module at project root;
  • Dropped support for Django < 1.6;
  • Added support for Django 1.9;

Version 1.1.0 - 2015/08/18

  • Do not use django.utils.importlib anymore since it’s deprecated in Django>=1.7, instead use Python2.7 importlib module;
  • Added relevant meta datas for package;

Version 1.0.0 - 2014/08/12

  • Added url namespace support;
  • Changed setup.py to add requirement for Django>=1.5;
  • Fixed issue with root url that was ignored in the crumbs;
  • Dropped support for Django < 1.5;

Version 0.9.0 - 2014/01/26

  • Changed tuple form for crumb entry;
  • Updated README;
  • Fixed compatibilty with Django >= 1.5;
  • Fixed error too many values;
  • Fixed setup.py;
  • Fixed MANIFEST;