I have annoying problems using django-sitetree (version 0.5.1) for generating a navigation menu with Django. For testing purposes I tried to configure django-sitetree to make a sitetree for the “Polls” app (official Django tutorial). I configured it according to some instructions answering a prior question – but i ran into trouble using url patterns, which didn’t work for me.
So these are my url patterns for “Polls”, which are included in /polls (I don’t use trailing slashes):
urlpatterns = patterns('polls.views',
(r'^$', 'index'),
(r'^/(?P<poll_id>\d+)$', 'detail'),
(r'^/(?P<poll_id>\d+)/results$', 'results'),
(r'^/(?P<poll_id>\d+)/vote$', 'vote'),
)
I only configured the views index and detail like this via Django admin:
Title URL
My site /
+ Polls polls.views.index
+ Poll {{ poll.question } polls.views.detail poll.id
Which produces the following output using {% sitetree_menu from "maintree" include "trunk" %} on my main page /
Title URL
My site /
+ Polls /polls
+ Poll #unresolved
But what I am expecting is:
Title URL
My site /
+ Polls /polls
+ Poll foo /polls/1
+ Poll Test #2 /polls/2
I have already tried several things like using named views in the urls.py (as recommended in the documentation), but i don’t even get a simple {% sitetree_url %} working with parameters. It’s a pity to see that there’s nothing helpful on the web except some copies of the answer mentioned above. I would appreciate it if anyone helped me out with a more detailed one. Thanks for your help!
Currently there is no way for the application to meet your expectations, i.e. it can’t figure out how many polls you have described by “Poll {{ poll.question }” of yours, and which of them you want to show in the menu.
I should try to think this feature over, but frankly speaking, I do not consider placing dozens of polls in the menu a good idea.
Nevertheless, there are several ways you can cope with it:
Create entries for each poll you need manually;
Add poll entry to the tree when new poll is created, e.g. using signals and model’s API. As you can see, it is the same as option 1, but automatized;
Do not show “Poll {{ poll.question }” in the menu and sitetree, but only in breadcrumbs. Thus considering “Polls” page for navigation through polls list. In case of breadcrumbs “Poll {{ poll.question }” will be resolved into full title when on pool’s page according to given rules.
For hardyharzen, who asks in the comments for an example on no.2:
You can read about signals at https://docs.djangoproject.com/en/1.3/topics/signals/.
Django built-in signals we will use are described at https://docs.djangoproject.com/en/1.3/ref/signals/.
Suppose we are still talking about polls. Put the following code in polls app models.py file.
Note that this code adds tree item into site tree with id 1, at tree branch with id 10, and expects Poll model to have get_absolute_url() method defined.
–