A flask-style `route` decorator for django views
Project description
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-decorator
It'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):
...
@route
def bar_view(request):
...
And 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`.
================================================
> 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-decorator
It'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):
...
@route
def bar_view(request):
...
And 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`.
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
Close
Hashes for django-route-decorator-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa10b95a0c512f479050164b69911ce45420f02c1b9a8d4e05e4f42d261f2983 |
|
MD5 | de56401674505613808af6efc3bbaffa |
|
BLAKE2b-256 | ee9b63b78b0b67a1bde0b2f7b5b206a96d6b71c0882952d7af4f2b2a6c171b2b |