All you need to know before trying Django-Routify only that you must use Router as an instance in your views.py file and include_router function with router object as a parameter in urlpatterns in your urls.py file.
Importing Router class from django_routify package, creating router instance of Router class with parameters '/app' as prefix, 'app' as app_name, and auto_trailing_slash=True and decorating hello_world view with '/hello-world' as url_path, GET HTTP method in methods, and auto_naming(view will be automatically named as 'hello_world'):
~/project/app/views.py
from django.http import HttpRequest, HttpResponse
from django_routify import Router
router = Router('/app', 'app', auto_trailing_slash=True)
@router.route('/hello-world', methods=['GET'])
def hello_world(request: HttpRequest) -> HttpResponse:
return HttpResponse('Hello World!')
Importing and registration router instance using include_router function from django_routify package:
~/project/app/urls.py
from django_routify import include_router
from .views import router
urlpatterns = [
include_router(router),
]
Writing hello_world function-based view and decorating it with require_http_methods GET method:
~/project/app/views.py
from django.http import HttpRequest, HttpResponse
from django.views.decorators.http import require_http_methods
@require_http_methods(['GET'])
def hello_world(request: HttpRequest) -> HttpResponse:
return HttpResponse('Hello World!')
Manually adding 'app' as app_name, creating '/app' as prefix for URLResolver paths and including hello_world view (all those were registered manually):
~/project/app/urls.py
from django.urls import path, include
from .views import hello_world
app_name = 'app'
urlpatterns = [
path(
'app/',
include(
[
path('hello-world/', hello_world, name='hello_world'),
]
),
),
]
As you can see, it's easier to use the django-routify package than to write one manually :)
For extended example with tests visit example app.