About

For a large Django project with a lot of views and probably a lot of REST endpoints (Not everyone uses GrpahQL or JSON:API) the urls.py file will be big, very big (assuming you not splitting your logic to smaller file and include them).

Django Decorated routers takes (basically steel it) and idea from the Symfomy project, an MVC framework in PHP, which the routers can be declared by using annotation on the class. Since Python has built in annotation in the language (while symfony puts the annotation on the docblock of the function and then parsing it) the way to implement is pretty basic: Django Decorated Routers looks for all the classes which as the proper decorator (in which you can read in the Register Routes section). All the decorated classes will be automatically will be add to the urlpatterns variable in which you usually add the routes pattern.

You probably wonder to your self - where this come in handy? Well, there are a couple of reasons:

  1. Small footprint in the urls.py file - once all your routes for the views are being added automatically, the url file will be no more than 10-15 lines. It's probably amazing consider that a file that for large project can get to something like 100 lines.

  2. One place to rule them all - Usually, when defining a new endpoint or a route it's happens in two locations - the urls.py file and the class view itself which eventually maintain in somewhere else and it's pretty sucks to maintain both of the files. When using Django decorated routes the route definition and the view happens in one place which help us to understand which route will trigger the view class we currently view and vice versa.

Last updated