Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda