Django实现装饰器注册路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def action(route, name=""): def inner_warpper(func): from django.urls import get_resolver resolver = get_resolver() if isclass(func) and issubclass(func, View): resolver.url_patterns.append(path(f"{route}/", func.as_view(), name=name if name else None)) else: resolver.url_patterns.append(path(f"{route}/", func, name=name if name else None)) @wraps(func) def wrapper(request, *args, **kwargs): return func(request, *args, **kwargs) return wrapper return inner_warpper 使用方法: 1 2 3 4 5 6 7 8 @action("classview") class TestView(View): def get(self, request): return JsonResponse({"message": "Hello, world!...