Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imbolc/django-route-decorator
A flask-style `route` decorator for django views
https://github.com/imbolc/django-route-decorator
django router
Last synced: 4 days ago
JSON representation
A flask-style `route` decorator for django views
- Host: GitHub
- URL: https://github.com/imbolc/django-route-decorator
- Owner: imbolc
- Created: 2018-06-19T06:01:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-21T23:10:15.000Z (11 months ago)
- Last Synced: 2024-11-11T20:44:50.447Z (5 days ago)
- Topics: django, router
- Language: Python
- Size: 3.91 KB
- Stars: 12
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A flask-style `route` decorator for django views
================================================> Simple things should be simple, complex things should be possible (c)Alan Kay
There is some contradiction between decoupling and DRY principles
in django urls. Why not use flasky `@route` decorator to fix this issue?Install
-------
pip install django-route-decoratorIt's compatible with django >= 2.0 only,
because it uses new `urls.path` syntax.Simple things should be simple
------------------------------
Just decorate your views with an instance of `Route`:from route_decorator import Route
route = Route()
@route
def foo_view(request):
# /foo-view@route
def bar__view(request):
# double underscore means a folder
# /bar/viewAnd don't forget to add the routes into your `urls.py`:
from . import views
urlpatterns += views.route.patterns
Now you have your views binded to `/foo-view` and `/bar-view` respectively.
You can also get map urlname -> url with `route.names` and pass them to
frontend maybe. In our case it would be:{
'foo_view': '/foo-view',
'bar__view': '/bar/view'
}Complex things should be possible
---------------------------------
You can pass `url_prefix` and `name_prefix` to route:route = Router('/api', 'api:')
And also use `path` and `name` in a decorator:
@route('/baz', 'baz-name')
def baz_view(request):
...So it would be bind to `/api/baz` with name `api:baz-name`.