A library for creating routes in Django with a similar structure to Next.js App Router.
- Dynamic routing
- Nested routing
- Route parameters
- Route groups
- Python 3.8+
- Django 4.2+
Windows:
$ pip install -U django-app-router
Linux/MacOS:
$ python3 -m pip install -U django-app-router
urls.py:
from django_app_router import routers
router = routers.AppRouter()
router.include_app('your_app') # app directory name
urlpatterns = [
# ...
]
urlpatterns += router.urls
Alternatively, you can use the include
function. like this:
urlpatterns = [
# ...
path('', include(router.urls)),
]
for example, define a route with the file page.py
in the routers
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} |
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.
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',
],
# ...
},
]
This project is licensed under the MIT License - see the LICENSE file for details.