Skip to main content

Routing

To set up routing for URLs in your Django application, you can use the Router class provided by the django_petra.router module. Below is an example of how to configure routing using the Router class.

Router

moduels/{module_name}/urls.py
from django.urls import path 
from modules.user.views import UserViewset
from django_petra.router import Router, Route

routes = [
Router.get('get/', UserViewset.get_users),
Router.post('add/', UserViewset.add_user),
# Router.put()
# Router.patch()
# Router.delete()
]

urlpatterns = [eval(route) for route in Route(routes).get_all_routes()]

params

  • you can use angle brackets <> for params
  • Router.get('get/<str:user_id>', UserViewset.get_user),
  • converter lists - str,int,slug(this-is-slug),uuid(075194d3-6885-417e-a8a8-6c931e272f00)
NOTE
  • No need leading slash