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).
Links¶
- Read the documentation on Read the docs;
- Download its PyPi package;
- Clone it on its Github repository;
User’s Guide¶
Install¶
pip install autobreadcrumbs
In your settings file add autobreadcrumbs to your installed apps:
INSTALLED_APPS = ( ... 'autobreadcrumbs', ... )
Import default settings:
from autobreadcrumbs.settings import *
Register its context processor:
TEMPLATE_CONTEXT_PROCESSORS = ( ... 'autobreadcrumbs.context_processors.AutoBreadcrumbsContext', ... )
(Order with other context processors don’t matter).
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 insettings.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 %}
Template string for separator item in tag
{% autobreadcrumbs_links %}
Optional project level crumbs file, this have to be a correct Python path to a crumb module.
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.
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 %}
Template tag to output breadcrumb title from current ressource crumb.
Example
{% load autobreadcrumb %} {% current_title_from_breadcrumbs %}
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
andAUTOBREADCRUMBS_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¶
Breadcrumbs site registry
Keyword Arguments: initial (dict) – Optional initial dictionnary of crumbs urlname->value
. Default to an empty dict.Return registred crumb url names.
Returns: List of registred crumb url names, sorted with default sorted()
behavior.Return type: list
Return current registry
Returns: Currrent registry. Return type: dict
Get title for given url name.
Parameters: name (string) – Url name. Returns: Crumb title. Return type: string or tuple
Find if given name is registred as a crumb.
Returns: True
if name exists in current registry, elseFalse
.Return type: bool
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 incrumbs.
Reset registry to an empty Dict.
Unregister a crumb.
Parameters: name (string) – Url name. Raises: NotRegistered
if given url name is not registred yet.
Update many crumbs
This works like the
Dict.update({..})
method.Parameters: crumbs (dict) – A dict of crumbs ( urlname->value
).
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.
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 insettings.INSTALLED_APPS
.Keyword Arguments: filename (string) – Module filename to search for. Default to crumbs
, so it will search for acrumbs.py
file at root of every enabled module fromsettings.INSTALLED_APPS
.Returns: List of successfully loaded Python paths. Return type: list
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 toNone
.Raises: Exception
– Raise any occuring exception from loaded Python path.Returns: Python path ( module.filename
) for discovered crumbs module. Iffilename
does not exists in module, returnNone
.Return type: string or None
Breadcrumb resolving¶
Simple crumb ressource model to contain all datas about a ressource.
Resolve given path as breadcrumbs
Parameters: root_urlconf (string) – Python path to an url conf file, usually settings.ROOT_URLCONF
. It will be used as the url map to resolve every given path.Cut given path into segments
Parameters: path (string) – An url path like /foo/bar/
.Returns: List of path segments, each segment is a part of the url path starting from /
and ending on the full path.Such as for
/foo/bar/
segments will be:- / - /foo - /foo/bar
Return type: list
Manage title format
Parameters: - name (string) – Url name.
- value (string) – Crumb value.
Keyword Arguments: request (django.http.request.HttpRequest) – Optional Django request object used with custom crumb function. If not given, crumb functions is ignored (so the crumb ressource still be available).
Returns: Crumb title.
Return type: string
Return current Breadcrumb from elements.
This is pretty simple as the current element is allways the last element (if element list is not empty).
Parameters: elements (list) – List of breadcrumb elements. Returns: The last element from given elements
if any, else None.Return type: BreadcrumbRessource or None
Return resolved breadcrumbs datas from given path.
Cut the path in segments and check each of them to find breadcrumb details if any.
Crumb value can be a simple string, a Django lazy unicode or a tuple
(title, custom_function)
.Crumb
custom_function
take url name and request object as arguments and will returnFalse
to ignore crumb (won’t be in breadcrumbs) orTrue
to keep crumb element.Parameters: path (string) – An url path like /foo/bar/
.Keyword Arguments: request (django.http.request.HttpRequest) – Optional Django request object used with custom crumb function. If not given, crumb functions will be ignored (so the crumb ressources still be available). Returns: Datas from resolved crumbs: autobreadcrumbs_elements
: Resolved bread crumbs for each segment;autobreadcrumbs_current
: Breadcrumb for current (the last one) path.
Return type: Dict
Template context processor¶
Context processor to resolve breadcrumbs from current ressource.
Use
request.path
to know the current ressource url path andsettings.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
todiscover.py
; - Moved regitry stuff from
__init__.py
toregistry.py
; - Better docstring for code;
- Many minor changes to follow Flake8;
- Added
AUTOBREADCRUMBS_ROOT_CRUMB
setting to be able to use acrumbs.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;