-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve request kwargs while defining MenuItem #70
Comments
I've made it by customizing the from django.http import HttpRequest
from django.urls import reverse
from menu import MenuItem, Menu
from django.utils.translation import gettext as _
def resolve_url(request: HttpRequest, view_name: str):
return reverse(view_name, args=[request.resolver_match.kwargs.get('pk'), ])
class CustomMenuItem(MenuItem):
def process(self, request):
# evaluate our url
if callable(self.url):
self.url = self.url(request, self.view_name)
super().process(request)
children = (
CustomMenuItem(_("Edit Group"),
resolve_url,
weight=10,
view_name='structure:group_edit'),
CustomMenuItem(_("Delete Group"),
resolve_url,
weight=20,
view_name='structure:group_remove',),
CustomMenuItem(_("Members"),
resolve_url,
weight=30,
view_name='structure:group_members',),
CustomMenuItem(_("Publishes for"),
resolve_url,
weight=40,
view_name='structure:group_publisher_overview'),
)
Menu.add_item('group_details', CustomMenuItem(_("Details"),
resolve_url,
weight=10,
children=children,
view_name='structure:group_details')) I'm wondering about that this isn't already implemented. Maybe a PR with the enhancement of a simple check if |
I tried to do something similar, and ran into the same issue that def children(request):
pk = request.resolver_match.kwargs.get('pk')
return [
CustomMenuItem(_("Edit Group"),
reverse('structure:group_edit', args=[pk]),
weight=10,
view_name='structure:group_edit'),
...
] I think your custom class is required for callable URLs at the top of the hierarchy, though. |
Nice. Pretty good solution to define the children's, cause you could also drop the subclass
Yep - Cause there is no callable check. If so, we could adapt your solution again, without defining the |
related to #19
I struggled also about the issue to catch the current
pk
attribute from the current request and pass it into the menu.I've got the following
urls.py
for example:How can i resolve the current requested
pk
from the request kwargs for example in themenus.py
:The text was updated successfully, but these errors were encountered: