Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
Custom authentication middleware for specific routes in Django

I implemented a custom authentication setup for my Django project. There are some user roles for users. Now I want to ensure that some specific routs may acceptable only of specific user roles. Let's say the edit/uploaded-files can be acceptable only for the user with role = 1. So I created a middleware for that.

from django.shortcuts import redirect

class HRMiddleware(object):
    def process_request(self, request):
        user = request.user
        if not (user and user.is_authenticated() and user.email):
            return redirect('')
        if user.role_id.id != 1:
            raise 403
        return None

Now how can i apply this middleware for some specific routes only ? I found some solutions like using decorator @decorator_from_middleware(MyMiddleware) and specifying the routes in middle ware. Is there any better way to do this ? Actually I am a Laravel developer. This is my first Django project. In laravel we can specify the middlewares in the routes. Please help me

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this:

URLS = ['/some_path/']

class HRMiddleware(object):
    def process_request(self, request):
        user = request.user
        if not (user and user.is_authenticated() and user.email) and request.path in URLS:
            return redirect('')
        if user.role_id.id != 1:
            raise 403
        return None
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda