Skip to main content

A django menu generator

Project description

Gaston

A django menu generator.

CSS Classes

Menu

A depth-{{ depth_count }} CSS class is added to every <ul>, indicating its depth, 0-indexed.

Submenu

Gaston automatically adds the following CSS classes to your MenuItem elements:

class description
active If the current URL exactly matches the MenuItem's URL
selected If the current URL contains the MenuItem's url
If any submenu MenuItem's url exatly matches the current URL

Menu Items

Gaston defines a MenuItem class. Each MenuItem accepts the following parameters:

Parameter Decsription
label The label to be displayed.
url The URL of the menu item.
css_classes Additional CSS classes for the specific element.
submenu A list of MenuItem that will be rendered as a nested <ul>.

Available tempaltetags

Gaston provides the following templatetags:

tag description
menu Renders a given menu.
get_item_css_classes Returns a string with the item's CSS classes.

Usage

Installation

  • install django-gaston

  • Add gaston to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    "gaston",
    ...
]

Definitions

  • Define a menus.py with your menu layout:
from django.urls import reverse

from gaston.menu import MenuItem

about_menu = [
    MenuItem("Company", url=reverse("company")),
    MenuItem("FAQ", url=reverse("faq")),
]

main_menu = [
    MenuItem("Home", url=reverse("home")),
    MenuItem("About", url=reverse("about"), css_classes="with-submenu", submenu=about_menu),
]
  • Define a context processor inside an app (e.g. PROJECT_NAME/context_processors.py):
from .menus import main_menu


def menus(request):
    return {
        "main_menu": main_menu,
    }
  • Add the above context processor in your list of context processors (in settings.py):
TEMPLATES = [
    {
        ...
        "OPTIONS": {
            "context_processors": [
                ...
                "APP_NAME.context_processors.menus",
            ],
        },
    },
]

Where APP_NAME is the name of the app where you defined your menus.py.

Templates

Finally, load gaston_tags inside your template and call it as follows:

{% load gaston_tags %}

<nav>{% menu main_menu %}</nav>

Where main_menu is the key from your context processor dictionary.

Build and Publish

Build

In the root folder run:

python3 setup.py sdist bdist_wheel

This will create a folder named dist with the code ready to be published to pypi.

Publish to PYPI

Install twine with pipx by running

pipx install twine

Run the command bellow to publish the package

twine upload dist/*

Username and Password will be asked. For username, use __token__ and for password the token that was generated in the PYPI site.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-gaston-0.4.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_gaston-0.4.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file django-gaston-0.4.1.tar.gz.

File metadata

  • Download URL: django-gaston-0.4.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for django-gaston-0.4.1.tar.gz
Algorithm Hash digest
SHA256 99443b2429786bf78a624f0b44ae5eded85c8b5decdb3d0c5b31f32602cf478c
MD5 000dc4750340f8945a044ea2e767102a
BLAKE2b-256 e57c98fc5160a41de52aa8f3b774d1a55ffa1ce2c09114e396d5d122cba2ac57

See more details on using hashes here.

File details

Details for the file django_gaston-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: django_gaston-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for django_gaston-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8144618c3a08f2c308c5d75275d05946bd77070c29a501da4f2894fe3f7d2cd
MD5 55de91d4b28dbcf46eb48f8e798a4d9b
BLAKE2b-256 096d63b3ef02b78d1a0fad50c4c05d027e74d39f782132dc700ca0fdaad3a786

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page