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

241
Vistas
How to ask for login to access to any url django

I have thies in views.py

 @login_required()
 def dadmin(request):

I had not used @login_required() in dapost and dapage

And in url.py

url(r'^dadmin/$', dadmin, name='dadmin'),
url(r'^dadmin/post/$', dapost, name='dapost'),
url(r'^dadmin/page/$', dapage, name='dapage'),

Now I want is everytime when users try to access domain.com/dadmin/any... it redirect to login page. how ca i Do that? without placing @login_required() in dapost and dapage?

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

0

you can do it by using custom middleware

save this as custom_middleware.py file in your main app

from django.shortcuts import redirect


class CheckUser(object):

    def __init__(self, get_response):

        self.get_response = get_response

    def __call__(self, request):

        if not request.user.is_authenticated() and \
                        request.path.startswith('/dadmin/'):
            return redirect("/login/")

        response = self.get_response(request)


        return response

and in your settings.py edit the middleware

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    #custom middlewares
    'app.custom_middleware.CheckUser'
]
over 4 years ago · Santiago Trujillo Denunciar

0

If you set LOGIN_URL in your settings.py:

https://docs.djangoproject.com/en/1.8/ref/settings/#login-url

all anonymous users will be redirected when accessing to urls with login_required() decorator in their views.

Edit: you can make a custom LoginRequiredMiddleware too, with the path you desire.

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