Complete worked example
python
from bootstrap_lab import django_setup
django_setup(__name__)
from django.http import JsonResponse
from django.test import Client
from django.urls import path, reverse
def detail(request, role_id):
return JsonResponse({"id": role_id})
urlpatterns = [path("roles/<int:role_id>/", detail, name="role-detail")]
def route(role_id):
if type(role_id) is not int or role_id < 0:
raise ValueError("nonnegative integer required")
url = reverse("role-detail", args=[role_id])
response = Client().get(url)
return {"path": url, "id": response.json()["id"]}- Reasoning
- The route captures role_id with the int converter, and reverse uses the named route to build a path. The function then sends a real test-client request and returns the generated path alongside the JSON value observed by the view.