Skip to content

Add programmatic nav construction and callable factory support#214

Merged
joshuadavidthomas merged 3 commits intomainfrom
claude/programmatic-nav-items-KEjcf
Feb 28, 2026
Merged

Add programmatic nav construction and callable factory support#214
joshuadavidthomas merged 3 commits intomainfrom
claude/programmatic-nav-items-KEjcf

Conversation

@joshuadavidthomas
Copy link
Owner

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.

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
@joshuadavidthomas joshuadavidthomas requested review from a team as code owners February 27, 2026 15:28

def main_nav(request: HttpRequest) -> Nav:
items = [NavItem(title="Home", url="/")]
if request.user.is_authenticated:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was tripping me up for some reason. I thought there was already a built-in "not authed", but never found it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do it now by passing a callable in the permissions list - https://django-simple-nav.westervelt.dev/en/latest/usage.html#permissions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's where I landed too.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@joshuadavidthomas joshuadavidthomas merged commit 2a7cfb4 into main Feb 28, 2026
22 checks passed
@joshuadavidthomas joshuadavidthomas deleted the claude/programmatic-nav-items-KEjcf branch February 28, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants