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

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

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 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