Skip to main content

A simple Django app to route requests, Inspired by Next.js App Router.

Project description

django app router

A modern, easy to use, and powerful router for Django apps. Inspired by the Next.js app router.

Features

  • Dynamic routing
  • Nested routing
  • Route parameters
  • Route groups

Requirements

  • Python 3.8+
  • Django 4.2+

Installing

Windows:

$ pip install -U django-app-router

Linux/MacOS:

$ python3 -m pip install -U django-app-router

Setup and Usage

urls.py:

from django_app_router import routers

router = routers.AppRouter()
router.add_app('your_app') # app directory name

urlpatterns = [
    # ...
]

urlpatterns += router.urls

Alternatively, you can use the include function. like this:

urlpatterns = [
    # ...
    path('', include(router.urls)),
]

Example

for example, define a route with the file page.py in the templates folder:

from django.shortcuts import render

def page(request):
    """home"""
    # You can also name the page
    # path(..., ..., name='home')

    return render(request, 'page.html')
Route Example URL params
routers/page.py / {}
routers/info/page.py /info/ {}
routers/(group)/about/page.py /about/ {}
routers/user/[slug]/page.py /user/1/ {'slug': 1}

Example folder structure

your_app
├── migrations
│   └── __init__.py
├── routers
│   ├── (auth)
│   │   ├── login
│   │   │   ├── page.html
│   │   │   └── page.py
│   │   └── register
│   │       ├── page.html
│   │       └── page.py
│   ├── info
│   │   └── page.py
│   ├── user
│   │   └── [user_id]
│   │       ├── page.html
│   │       └── page.py
│   ├── layout.html
│   ├── page.html
│   └── page.py
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── urls.py
└── views.py

You can see the full example in the example folder.

Notes

if you have .html files in the routers directory. ensure that the DIRS setting in the TEMPLATES setting includes the routers directory.

TEMPLATES = [
    {
        # ...
        'DIRS': [
            # any other directories
            BASE_DIR / 'your_app' / 'routers',
        ],
        # ...
    },
]

License

This project is licensed under the MIT License - see the LICENSE file for details.

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-app-router-0.1.0a0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

django_app_router-0.1.0a0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file django-app-router-0.1.0a0.tar.gz.

File metadata

  • Download URL: django-app-router-0.1.0a0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for django-app-router-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 6010340f94a568762156bfb6ccbd7ad3f3d889483e07c224a5be14c3fa5a2205
MD5 f5f226b8a62bcfac89d6eba84db282b0
BLAKE2b-256 9ddb50a2f4c420cc116aed311169d51751aae7725861d16721bc3ae5ce501783

See more details on using hashes here.

File details

Details for the file django_app_router-0.1.0a0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_app_router-0.1.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 a28c2fa84caf070effb34d1fc74c0bac42ae0a5aac1c666f0ff54c823af98d96
MD5 3ce20b1f12ff18ae84432353d4e5412d
BLAKE2b-256 b5748cd0cde90023032a84a5e00c2b2640b2116de4c2031594a94055950b1ba0

See more details on using hashes here.

Supported by

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