Getting started¶
Add the sitetree application to INSTALLED_APPS in your settings file (usually ‘settings.py’).
Check that django.core.context_processors.request is added to TEMPLATE_CONTEXT_PROCESSORS in your settings file. For Django 1.8+: django.template.context_processors.request should be defined in
TEMPLATES/OPTIONS/context_processors
.Check that django.contrib.auth.context_processors.auth is enabled in TEMPLATE_CONTEXT_PROCESSORS too.
Run
./manage.py migrate
to install sitetree tables into database.Go to Django Admin site and add some trees and tree items (see Making tree section).
Add {% load sitetree %} tag to the top of a template.
- Now you can use the following template tags:
sitetree_menu - to render menu based on sitetree;
sitetree_breadcrumbs - to render breadcrumbs path based on sitetree;
sitetree_tree - to render site tree;
sitetree_page_title - to render current page title resolved against definite sitetree.
sitetree_page_description - to render current page description resolved against definite sitetree.
Making a tree¶
Taken from StackOverflow.
In this tutorial we create a sitetree that could handle URI like /categoryname/entryname.
To create a tree:
Go to site administration panel;
Click +Add near ‘Site Trees’;
Enter alias for your sitetree, e.g. ‘maintree’. You’ll address your tree by this alias in template tags;
Push ‘Add Site Tree Item’;
Create the first item:
Parent - As it is root item that would have no parent. Title - Let it be 'My site'. URL - This URL is static, so put here '/'.
Create a second item (that one would handle ‘categoryname’ from your ‘categoryname/entryname’):
Parent - Choose 'My site' item from step 5. Title - Put here 'Category #{{ category.id }}'. URL - Put named URL 'category-detailed category.name'. In 'Additional settings': check 'URL as Pattern' checkbox.
Create a third item (that one would handle ‘entryname’ from your ‘categoryname/entryname’):
Parent - Choose 'Category #{{ category.id }}' item from step 6. Title - Put here 'Entry #{{ entry.id }}'. URL - Put named URL 'entry-detailed category.name entry.name'. In 'Additional settings': check 'URL as Pattern' checkbox.
Put ‘{% load sitetree %}’ into your template to have access to sitetree tags.
Put ‘{% sitetree_menu from “maintree” include “trunk” %}’ into your template to render menu from tree trunk.
Put ‘{% sitetree_breadcrumbs from “maintree” %}’ into your template to render breadcrumbs.
Steps 6 and 7 clarifications:
In titles we use Django template variables, which would be resolved just like they do in your templates.
E.g.: You made your view for ‘categoryname’ (let’s call it ‘detailed_category’) to pass category object into template as ‘category’ variable. Suppose that category object has ‘id’ property. In your template you use ‘{{ category.id }}’ to render id. And we do just the same for site tree item in step 6.
In URLs we use Django’s named URL patterns (documentation). That is almost idential to usage of Django ‘url’ tag in templates.
Your urls configuration for steps 6, 7 supposed to include:
url(r'^(?P<category_name>\S+)/(?P<entry_name>\S+)/$', 'detailed_entry', name='entry-detailed'), url(r'^(?P<category_name>\S+)/$', 'detailed_category', name='category-detailed'),Consider ‘name’ argument values of ‘url’ function.
So, putting ‘entry-detailed category.name entry.name’ in step 7 into URL field we tell sitetree to associate that sitetree item with URL named ‘entry-detailed’, passing to it category_name and entry_name parameters.