django-sitetree icon indicating copy to clipboard operation
django-sitetree copied to clipboard

Menu Components only appear when the current page

Open dwasyl opened this issue 3 years ago • 4 comments

Hi there,

I'm trying to have a sidebar menu that shows a number of items and the current page for a CRUD interface.

For example:

Items
  - Add Item
  - Item List

Then when I'm viewing an item sitemenu adds:

    - Item Name

Based on: item("{{ form.instance.name }}", "obj_view form.instance.pk", in_sitetree=True, in_menu=True),

But, then when I'm editing an item I want it to show:

   - Item Name

Based on: item("{{ form.instance.name }}", "obj_edit form.instance.pk", in_sitetree=True, in_menu=True),

Although, only one of them should appear at any given time.

I've tried adding them as separate entries on the tree, resolving to the dynamic url which works great. Except since the edit url path is a subpath from the 'view' url so both end up appearing.

Is there anyway to only show a particular item on the tree when the page is being accessed?

This also happens, when there are multiple paths to the same page. For example, when a menu with these entries:

item("{{ form.instance.obj }}", "obj_edit form.instance.obj.pk form.instance.pk", in_sitetree=True),
item("{{ form.instance.obj }}", "obj_edit form.instance.pk", in_sitetree=True),

This is based on including {% sitetree_menu from "main-tree" include "this-parent-siblings" %} in the template (so I need to have this included in the menu, but then I get an extra menu entry.

dwasyl avatar Jul 28 '20 18:07 dwasyl

Hi,

Sorry for the delay.

Is there anyway to only show a particular item on the tree when the page is being accessed?

From your description I understand that there are no universal criteria to distinguish such cases in general. Thing get even harder since the edit url path is a subpath from the 'view' url. So logic probably should go into tree items hook or in a custom template.

Yet I'm failing to reason about why do you want, being on edit page, show that very page in navigation. The other approach would be showing on edit page breadcrumbs, leading to details page. In that case link to edit would be on details page.

idlesign avatar Jul 30 '20 11:07 idlesign

@idlesign Sorry, that was a bit muddle an explanation of what I was looking to do.

I have a site menu along these lines:

Library
   - Add Book
   - Book List

Whenever a user accesses the book list and either views or edits a book I was hoping the menu would add:

Library
   - Add Book
   - Book List
    - {{ current book name }}

Whether the user is editing or viewing the book, I wanted only a single menu item to appear. To do that I have a menu setup like this:

item('Library', 'library_home', children=[
    item("Add Book", "book_add"),
    item("Book List", "book_list"),
    item("{{ obj }}", "book_view obj.pk"),
    item("{{ obj }}", "book_view obj.slug"),
    item("{{ obj }}", "book_edit obj.pk"),
    item("{{ obj }}", "book_edit obj.slug"),
])

The urls.py path is something like this:

re_path(r'^books/(?P<pk>\d+)/$', views.book_view, name='book_view'),
re_path(r'^books/(?P<slug>\w-+)/$', views.book_view, name='book_view'),
re_path(r'^books/(?P<pk>\d+)/edit/$', views.book_edit, name='book_edit'),
re_path(r'^books/(?P<slug>\w-+)/edit/$', views.book_edit, name='book_edit'),

However, since both the edit and view pages have a obj in the context sitetree is able to resolve both URLs, both appear in the menu. One other thing I tried was just not having a menu item for the edit page, but since I display the menu like this-parent-siblings, when I do that no menu appears (because this doesn't resolve to any menu item).

The navigation is a sidebar (rather than a dropdown menu) so the idea of showing it was simply as an reminder of the current page. Originally, I wanted to treat the view/edit pages as if they were still on the Book List page, but that didn't work because of this-parent-siblings.

Having written this out, perhaps I'm just going about this entirely the wrong way, and should make the views more divergent in context objects, or give up on showing the current object in the sidebar and settle for a more fixed menu, rather than one that starts with this.

dwasyl avatar Jul 30 '20 19:07 dwasyl

You may want to give a try to breadcrumbs as a reminder of the current page, as I mentioned above. Since there's an option to hide items from menu and breadcrumbs, this may suit your need.

idlesign avatar Jul 31 '20 00:07 idlesign

Thanks @idlesign. I've got breadcrumbs, this was just about the menuing and trying to have the current page appear "active" when using this. I'm going to plug away a bit more and see if there isn't a solution to the way menu items show up as active.

dwasyl avatar Aug 17 '20 16:08 dwasyl

Considered closed.

idlesign avatar Dec 22 '23 02:12 idlesign