Getting started¶
- Add the
sitetreeapplication toINSTALLED_APPSin your settings file (usuallysettings.py). -
Check that
django.core.context_processors.requestis added toTEMPLATE_CONTEXT_PROCESSORSin your settings file.Note
For Django 1.8+: it should be defined in
TEMPLATES/OPTIONS/context_processors. -
Check that
django.contrib.auth.context_processors.authis enabled inTEMPLATE_CONTEXT_PROCESSORStoo. - Run
./manage.py migrateto install sitetree tables into database. - Go to Django Admin site and add some trees and tree items.
- Add
{% load sitetree %}tag to the top of a template.
Making a tree¶
Taken from StackOverflow.
In this tutorial we create a sitetree that could handle URI like /categoryname/entryname.
To create a tree:
Note
Here we create a tree in Django admin. You can also define trees right in your code. See section on dynamic trees.
- Go to site administration panel;
- Click
+AddnearSite 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 beMy site.URL- This URL is static, so put here/.
-
Create a second item (that one would handle
categorynamefrom yourcategoryname/entryname):Parent- ChooseMy siteitem from step 5.Title- Put hereCategory #{{ category.id }}.URL- Put named URLcategory-detailed category.name.
In
Additional settings: checkURL as Patterncheckbox. -
Create a third item (that one would handle
entrynamefrom yourcategoryname/entryname):Parent- ChooseCategory #{{ category.id }}item from step 6.Title- Put hereEntry #{{ entry.id }}.URL- Put named URLentry-detailed category.name entry.name.
In
Additional settings: checkURL as Patterncheckbox. -
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 ascategoryvariable. Suppose that category object hasidproperty.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 identical to the usage of Django url tag in templates.
Your urls configuration for steps 6, 7 supposed to include:
Take a not on
nameargument values.
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.
Now you're ready to move to templates and use template tags.