Add programmatic nav construction and callable factory support#214
Add programmatic nav construction and callable factory support#214joshuadavidthomas merged 3 commits intomainfrom
Conversation
Enable two new patterns for building navigation dynamically: 1. Direct Nav() construction with template_name and items kwargs, allowing conditional item lists without subclassing. 2. Callable factory functions (accepting request, returning Nav) that can be referenced by dotted import path in template tags and Jinja2, enabling request-dependent nav building. The Nav class gains a custom __init__ that uses object.__setattr__ only when args are provided, preserving the existing subclass pattern where class attributes are not shadowed by instance defaults. https://claude.ai/code/session_012Aun5HwGX3yAM6nmRzThoe
|
|
||
| def main_nav(request: HttpRequest) -> Nav: | ||
| items = [NavItem(title="Home", url="/")] | ||
| if request.user.is_authenticated: |
There was a problem hiding this comment.
This was tripping me up for some reason. I thought there was already a built-in "not authed", but never found it.
There was a problem hiding this comment.
You can do it now by passing a callable in the permissions list - https://django-simple-nav.westervelt.dev/en/latest/usage.html#permissions
There was a problem hiding this comment.
That's where I landed too.
There was a problem hiding this comment.
Wouldn't passing "is_anonymous" work? On my phone so I can't look it up directly at the moment, but AFAIK that's a property on the User model.
There was a problem hiding this comment.
Wouldn't passing "is_anonymous" work? On my phone so I can't look it up directly at the moment, but AFAIK that's a property on the User model.
Well, you couldn't before but now you can #215
Enable two new patterns for building navigation dynamically:
The Nav class gains a custom init that uses object.setattr only when args are provided, preserving the existing subclass pattern where class attributes are not shadowed by instance defaults.