Register Routes

Django decorated routes is not opinionated but help you do something pretty simple: place your views any where you want. You need to follow two basic rules:

  1. Your class view must extends from the django.views.generic.base.View . Yes, views based on DRF base views are welcome.

  2. You need to add the decorator.

How to

Well, it's no rocket science. Have a look at the next peace of code:

from django.http import JsonResponse
from django.views.generic.base import View
from decorated_router.api.decorators import url_decoration


@url_decoration(path="api/blog/")
class Blog(View):
    def get(self, request):
        return JsonResponse({'foo': 'bar'})

That's mean that we hit the endpoint api/blog you'll get a nice JSON when foo equals bar.

In case you have path with regex pattern, or re_path or it defined in the documentation you can this:

@url_decoration(re_path=r'^api/test/blog/(?P<blog_id>\d+)/?$', name="blog")
class BlogControllerForTests(View):
    def get(self, request, blog_id):
        return JsonResponse({'id': 1, 'title': f'Nice Blog {blog_id}'})

If you did not notice, you can set a name for the route just like you would do without Django Decorated Routes.

Last updated