Correctly set django-axes for pythonanywhere

Hello everyone.
I have a small problem that unfortunately I couldn’t solve on my own.

I created my django project. This is not a very complicated project.

I installed “django-axes” and configured it following the appropriate documentation.

The problem is that once deployed on pythonanywhere.com, django-axes always detects the same IP address regardless of the real IP that logs in.

I tried to document myself and it seems that the problem is due to pythonanywhere balanced proxy.

Therefore, carefully reviewing the django-axes and pythonanywhere documentation, I tried to make the following changes:

settings.py:

INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'app1',
     'app2',
     'django_filters',
     'import_export',
     'djmoney',
     'crispy_forms',
     'crispy_bootstrap5',
     'axes',
]
MIDDLEWARE = [
     #the last middleware inserted:
     'axes.middleware.AxesMiddleware',
]

AUTHENTICATION_BACKENDS = [
     # AxesStandaloneBackend should be the first backend in the AUTHENTICATION_BACKENDS list.
     'axes.backends.AxesStandaloneBackend',
     # Django ModelBackend is the default authentication backend.
     'django.contrib.auth.backends.ModelBackend',
]

# refer to the Django request and response objects documentation
AXES_IPWARE_PROXY_COUNT = 1

AXES_IPWARE_META_PRECEDENCE_ORDER = [
     'HTTP_X_REAL_IP',
     'HTTP_X_FORWARDED_FOR',
     'REMOTE_ADDR',
]

I set ‘HTTP_X_REAL_IP’ first because it is specified in the pythonanywhere instructions:

Despite this, even if I try to change the order of the contents of “AXES_IPWARE_META_PRECEDENCE_ORDER”
or I remove the setting “AXES_IPWARE_PROXY_COUNT = 1”
the IP address reported by django-axes always remains the same.

What am I doing wrong?
Do I still need to configure anything?

Thank you all so much for your constant availability.
I accept every little advice and help that you are kindly able to give me.

Sorry for my bad English.

Happy Coding!

Hi everyone.
I finally managed to find the solution.
In practice, by reading the django axes documentation more carefully, I discovered that the latest version of django-axes (6.x.x), to function correctly with balanced proxies (like pythonanywhere) requires the django-ipware installation package (not most automatically installed together with django-axes).
So either run this command:

pip install django-ipware

I later changed my settings.py file like this:

INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'app1',
     'app2',
     'django_filters',
     'import_export',
     'djmoney',
     'crispy_forms',
     'crispy_bootstrap5',
     'axes',
]

MIDDLEWARE = [
    #the last middleware inserted:
    'axes.middleware.AxesMiddleware',
]

AUTHENTICATION_BACKENDS = [
    # AxesStandaloneBackend should be the first backend in the AUTHENTICATION_BACKENDS list.
    'axes.backends.AxesStandaloneBackend',

    # Django ModelBackend is the default authentication backend.
    'django.contrib.auth.backends.ModelBackend',
]

AXES_IPWARE_META_PRECEDENCE_ORDER = [
    'HTTP_X_REAL_IP'
]

Now django-axes detects an IP address, which is always different depending on the network you are connected to when you log in.
So I assume everything is working fine.

Thank you very much anyway everyone.
I hope it can help someone.

Happy Coding!