diff --git a/README.md b/README.md index af67646..74f4cf4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ -`django-simple-nav` is a Python/Django application designed to simplify the integration of navigation and menu bars in your Django projects. With a straightforward API and customizable options, you can easily add and manage navigational elements in your web applications. +`django-simple-nav` is a Python/Django application designed to simplify the integration of navigation and menu bars in your Django projects. With a straightforward API and customizable options, you can easily add and manage navigational elements in your web applications. It is designed to be simple to start with, but flexible enough to handle complex navigation structures while maintaining that same simplicity. ## Requirements @@ -16,13 +16,13 @@ ## Getting Started -1. Install the package from PyPI: +1. **Install the package from PyPI.** ```bash python -m pip install django-simple-nav ``` -2. **Add to Installed Apps**: +2. **Add `django_simple_nav` to `INSTALLED_APPS`.** After installation, add `django_simple_nav` to your `INSTALLED_APPS` in your Django settings: @@ -36,11 +36,33 @@ ## Usage -1. **Create a navigation definition**: +1. **Create a navigation definition.** - Define your navigation structure in a Python file. Here's an example configuration: + Define your navigation structure in a Python file. This file can be located anywhere in your Django project, provided it's importable. You can also split the navigations across multiple files if desired. + + A good starting point is to create a single `nav.py` or `navigation.py` file in your Django project's main configuration directory (where your `settings.py` file is located). + + `django-simple-nav` provides three classes to help you define your navigation structure: + + - `Nav`: The main container for a navigation structure. It has two required attributes: + - `template_name`: The name of the template to render the navigation structure. + - `items`: A list of `NavItem` or `NavGroup` objects that represent the navigation structure. + - `NavGroup`: A container for a group of `NavItem` or `NavGroup` objects. It has two required and three optional attributes: + - `title`: The title of the group. + - `items`: A list of `NavItem` or `NavGroup`objects that represent the structure of the group. + - `url` (optional): The URL of the group. If not provided, the group will not be a link but just a container for the items. + - `permissions` (optional): A list of permissions that control the visibility of the group. + - `extra_context` (optional): A dictionary of additional context to pass to the template when rendering the navigation. + - `NavItem`: A single navigation item. It has two required and three optional attributes: + - `title`: The title of the item. + - `url`: The URL of the item. This can be a URL string (e.g. `https://example.com/about/` or `/about/`) or a Django URL name (e.g. `about-view`). + - `permissions` (optional): A list of permissions that control the visibility of the item. + - `extra_context` (optional): A dictionary of additional context to pass to the template when rendering the navigation. + + Here's an example configuration: ```python + # config/nav.py from django_simple_nav.nav import Nav from django_simple_nav.nav import NavGroup from django_simple_nav.nav import NavItem @@ -61,6 +83,12 @@ NavItem(title="Internal Django URL by Name", url="fake-view"), ], ), + NavGroup( + title="Container Group", + items=[ + NavItem(title="Item", url="#"), + ], + ), NavItem( title="is_authenticated Item", url="#", permissions=["is_authenticated"] ), @@ -69,26 +97,98 @@ NavItem( title="myapp.django_perm Item", url="#", permissions=["myapp.django_perm"] ), + NavGroup( + title="Group with Extra Context", + items=[ + NavItem( + title="Item with Extra Context", + url="#", + extra_context={"foo": "bar"}, + ), + ], + extra_context={"baz": "qux"}, + ), ] ``` -2. **Integrate Navigation in Templates**: +2. **Create a template for the navigation.** + + Create a template to render the navigation structure. This is just a standard Django template so you can use any Django template features you like. + + Any items with permissions attached will automatically filtered out before rendering the template based on the request user's permissions, so you don't need to worry about that in your template. + + Items with extra context will have that context passed to the template when rendering the navigation, which you can access either directly or through the `item.extra_context` attribute. + + For example, given the above example `MainNav`, you could create a `main_nav.html` template: + + ```htmldjango + +