Reputation: 379
is there anyway to define middleware for specific route or route groups in django? like laravel which we can define it as follows:
Route::get('admin/profile', function () {})->middleware('auth');
Upvotes: 7
Views: 7866
Reputation: 1730
Historically there have been a few hooks you can exploit to do something like this. But nowadays you can for sure override:
def process_view(self, request, view_func, view_args, view_kwargs):
...
in your middleware class and then you could resolve the view/route from request.path
and dispatch custom logic if it matches or not, or you could match over view_func.__name__
or something similar, etc. Depends on your needs.
https://docs.djangoproject.com/en/2.0/topics/http/middleware/#process-view
Upvotes: 11