2

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!

Community
  • 1
  • 1
purefanatic
  • 933
  • 2
  • 8
  • 23

1 Answers1

1

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:

  1. Create entries for each poll you need manually;

  2. 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;

  3. 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:

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.

from django.db.models.signals import post_save  
from django.dispatch import receiver  
from sitetree.models import TreeItem

-

@receiver(post_save, sender=Poll)
def on_poll_add(instance, created, **kwargs)
    if created:
        tree_item = TreeItem(title=instance.title, tree_id=1, 
            parent_id=10, url=instance.get_absolute_url())  
        tree_item.save(force_insert=True)
idle sign
  • 1,164
  • 1
  • 12
  • 19
  • [django-newbie-speaking]: Would you please elaborate on 2.? This sounds like an elegant solution. – hardyharzen Oct 25 '11 at 15:29
  • Thank you for this detailed answer! I already figured out using the Poll in the menu or sitetree isn't really smart, and using the breadcrumbs navigation works just well. I don't think one needs to do things as I tried often. – purefanatic Oct 27 '11 at 21:37
  • I have a few minor questions: Is there any way to limit the nesting depth to a given number when using a sitetree or a menu? And if I use the breadcrumbs in my base template, I always get a TemplateError when extending it with an app for which no treeItem is defined. How to avoid this? It would be fine if you could make an exception using a boolean like treeitem_available in the template. – purefanatic Oct 28 '11 at 12:06
  • @purefanatic You can override built-in templates to limit the depth of rendered tree. To avoid TemplateErrors you can wrap `sitetree_breadcrumbs` tag in `block` tag, that for your app with no tree items defined you can replace with empty `block` tag. That is really a matter of templates juggling. – idle sign Oct 29 '11 at 03:53
  • As you can see, I don't have much experience with Django. I am trying to learn more about it. Thank you again for helping out! – purefanatic Oct 30 '11 at 14:19