Comments (4)
Thank you for the advices. I did something like the following (tried with tweaking the default "menu.html" template)
{% load sitetree %}
<ul>
{% for item in sitetree_items %}
<li>
<a href="{% sitetree_url for item %}" {% if item.hint %}title="{{ item.hint }}"{% endif %} {% if item.is_current or item.in_current_branch %}class="{{ item.is_current|yesno:"current_item ," }}{{ item.in_current_branch|yesno:"current_branch," }}"{% endif %}>{{ item.title_resolved }}</a>
{% if item.has_children %}
{# "mybooks" is a classical sitetree item, considered as the parent of the dynamic items #}
{% if item.alias == "mybooks" %}
{% include "sitetree/menu.html" with sitetree_items=custom_sitetree_items %}
{% else %}
{% sitetree_children of item for menu template "sitetree/menu.html" %}
{% endif %
{% endif %}
</li>
{% endfor %}
</ul>
With custom_sitetree_items
having been set in a view :
from sitetree.utils import item
[…]
context["indice_sitetree_items"] = item(
str(book),
f"book_detail {book.pk}"
)
for book in self.current_request.user.favorite_books[:5]
I think that does the job. Thank you !
from django-sitetree.
Hi,
Sorry for the delay.
When calling the template tag {% sitetree_menu from "tree" include "trunk" %}, how can I (and should I ?) pass the queryset allowing to generate the items "/book/1" to "/book/5" ?
There's currently no way to represent arbitrary model entities as tree items. In many cases it would ruin the navigation (taking into account that there may be hundreds of items).
Thus, if you want to see every book in your menu (that is, if there are only a few books) you'll need to add tree items either manually (throught the admin interface) or with a help of some code.
from django-sitetree.
Hi,
Thank you for the answer. I've tried to explore some code implementation to achieve this. In order to make my example here more accurate, the goal is not to display all the books in the menu, but to display the top 5 favorites books for each user.
Some ideas I've tried until now :
Adding through admin interface
That would mean that the menu is the same for everyone and cannot change automatically over time. But I want it to be user bound.
Dynamic tree structuring
As suggested in the docuementation :
somewhere where app registry is already created, e.g. config.ready() or even in urls.py of your project.
So, it is also here not bound to the user. For it, I would need the dynamic registration to be made at request time, in a view for instance. But I feel like it is not means for that since the trees are not "request-self-isolated" and are stored globally
Tree handler customization
That's the closest to what I want to achieve. I've created the following class and set it as SITETREE_CLS
from sitetree.sitetreeapp import SiteTree
from sitetree.utils import item
class CustomTree(SiteTree):
def apply_hook(self, items, sender):
if sender in ["menu", "menu.children"]:
# find the required "URL as pattern" treeitem by its alias.
index = next(
(
index
for (index, treeitem) in enumerate(items)
if treeitem.alias == "book_detail"
),
None,
)
if index is not None:
# create a set of new treeitems from a queryset result
injected_items = [
item(
str(book),
f"book_detail {book.pk}"
)
for book in self.current_request.user.favorite_books[:5]
]
# replace the original "URL as pattern" treeitem by the new set
items[index : index + 1] = injected_items
return items
It looks promising and generate new items that are indeed rendered in the html output. But
- The new injected items are not taken into account for marking them as
is_current
- Doing it manually in the apply_hook can work but then the
is_current_branch
doesn't propagate and it should also be done in an other branch of the same apply_hook (it is originally done here before the call to apply_hook) - Some other atrtibutes like title_resolved, depth are also not set
So instead of re-doing all the logic in this method, I'm thinking about to overwrite the attach_dynamic_tree_items method to make it attach an other kind of dynamic items (i.e. on each request).
Would you think that is a good approach ? I'm a bit concerned about the cache being written following the dynamic attachement.
from django-sitetree.
Now I see, thank you.
You're right: there's nothing user-specific (except permissions check). I'm afraid there's no easy way to put what you want into navigation structure. It'd probably mean that you need to override a considerable part of logic and disable caching.
Personally I would debate that something highly variable like top n entities (especially depending on current user) belongs to navigation. I'd rather not to put it under sitetree.
If you still want to make that, I'd propose you to override template(s), check there for a certain item alias and render its subitems manually. That'd seem to require least effort.
from django-sitetree.
Related Issues (20)
- Dynamic Trees - missing something? HOT 6
- Finer add permission check for displaying the "Add site tree Item" button HOT 3
- Incorporate one dynamic tree into another HOT 2
- Menu Components only appear when the current page HOT 5
- Django 3.1 compatibility HOT 2
- Tests are missing from the sdist HOT 1
- Matching this when multiple URL matches fails HOT 2
- Tree items not displayed in menu if url contains certain special characters HOT 4
- Query the AppConfig directly for its module name HOT 4
- NoReverseMatch on custom models HOT 5
- SiteTree class not returning current item HOT 1
- Sitetreedump with "tree" arguments don't work correctly HOT 1
- compose_dynamic_tree with access_by_perms called in `config.ready` results in database errors HOT 3
- Allow HTML in title_resolved (to allow font-awesome icons) HOT 4
- When changing DEFAULT_AUTO_FIELD setting there are pending migrations
- Building and using a dynamic menu HOT 2
- Dynamic tree items: allow custom attribute binding in item() HOT 1
- Dynamic tree items: simplified access check
- Mapping nested entities to bread crumbs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-sitetree.