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

445
Vistas
How do I get the user object from Django middleware?

I have the following middleware:

class TestMiddleware():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print(getattr(request, "user", None))
        return self.get_response(request)

The print function always prints None. I added the middleware both before and after the django.contrib.auth.middleware.AuthenticationMiddleware line in settings.py, but in either case, I get None. Also, I'm logged in as a user.

What else can I try? I'm using Django 1.11.

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

0

Your middleware should be after 'django.contrib.auth.middleware.AuthenticationMiddleware' if you want to access the user. You don't need to use getattr, just request.user should work. Depending on whether you are logged in or not, it will be the logged-in user or an AnonymousUser.

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