yunojuno / django-geoip2-extras

Additional functionality using the GeoIP2 database and functions.
MIT License
8 stars 4 forks source link

Middleware not working #11

Closed diego-rapoport closed 3 years ago

diego-rapoport commented 3 years ago

If anyone could help, please. I'm just trying to make some testing before using into a greater project, but couldn't make it work yet.

I'm using django 3.2, geoip2 3.0, geoip2-extras 1.2.1 and maxminddb 2.0

I have set the GEOIP_PATH to where the mmdb files from city and country are. Also have the GeoIP2Middleware after the SessionMiddleware in my settings. Still got no data from get request with a basic function view.

Anywhere I should look before start pasting blocks of code here? Suggestions of debug, maybe?

hugorodgerbrown commented 3 years ago

Hi @diego-rapoport - first thing is to ensure that the geoip2-extras logging is set to DEBUG. Then you should see, on startup, whether the middleware is active (it will report an error if it can't find the database(s)). If you still get nothing, then I would suggest printing the request.META.get("HTTP_X_FORWARDED_FOR") and request.META.get("REMOTE_ADDR") so that you know exactly what the client IP it is trying to lookup is.

If it's still failing, it needs to be reproducable, and we would need a test to demonstrate the exception.

hugorodgerbrown commented 3 years ago

@diego-rapoport I have pushed a v.1.3 which adds classifiers for Django 3.2 and Python 3.9, and some additional logging.

diego-rapoport commented 3 years ago

Hi @hugorodgerbrown. Thanks for the response.

First, how do I know geoip2-extras logging is set to DEBUG? My project is set to DEBUG and still nothing shows up about the middleware. I've printed the request.META.get("HTTP_X_FORWARDED_FOR") and request.META.get("REMOTE_ADDR") and so far got None for the first and the expected IP for the latter. Just tested with my own computer and my mobile phone using 0.0.0.0:8000

It's just not creating the request.geo_data at all.

hugorodgerbrown commented 3 years ago

The logging is the Django project logging. You can look at tests/settings.py for an example.

If your databases are not available, you should see something like this on startup:

django-geoip2-extras $ python manage.py runserver
Watching for file changes with StatReloader
INFO Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 29, 2021 - 09:05:45
Django version 3.2.6, using settings 'tests.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
DEBUG MiddlewareNotUsed('geoip2_extras.middleware.GeoIP2Middleware'): Error initialising GeoIP2: Could not load a database from [...].

If your database are found, you will see this on startup:

django-geoip2-extras $ python manage.py runserver
Watching for file changes with StatReloader
INFO Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 29, 2021 - 09:08:30
Django version 3.2.6, using settings 'tests.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
DEBUG Found GeoIP2 Country database
diego-rapoport commented 3 years ago

The logging is the Django project logging. You can look at tests/settings.py for an example.

If your databases are not available, you should see something like this on startup:

django-geoip2-extras $ python manage.py runserver
Watching for file changes with StatReloader
INFO Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 29, 2021 - 09:05:45
Django version 3.2.6, using settings 'tests.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
DEBUG MiddlewareNotUsed('geoip2_extras.middleware.GeoIP2Middleware'): Error initialising GeoIP2: Could not load a database from [...].

If your database are found, you will see this on startup:

django-geoip2-extras $ python manage.py runserver
Watching for file changes with StatReloader
INFO Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 29, 2021 - 09:08:30
Django version 3.2.6, using settings 'tests.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
DEBUG Found GeoIP2 Country database

DEBUG is indeed set and my database is found because it matched the second option.

I have a question. Does the middleware aware if the ip could not be resolved in some way by GeoIP2? If it does, how does it retrieve the information?

hugorodgerbrown commented 3 years ago

@diego-rapoport I have resurrected an old branch and republished it as a "v2" of this package. It changes how the package works, such that the GeoIP data is added to the request headers rather than the request object itself. The open PR is here - https://github.com/yunojuno/django-geoip2-extras/pull/12 - and I have published it to PyPI as 2.0b0. It's a pre-release so it won't install by default (depending on how you manage python dependencies).

This will be the main branch going forwards - it only supports Django 3.2+.

hugorodgerbrown commented 3 years ago

I have released v2 of the library - I would strongly recommend upgrading to that and trying again. In the meantime I am going to close the issue pending any further information. Please open a new issue against v2 if you still find yourself having issues.

diego-rapoport commented 3 years ago

Sorry the delay. Thanks for all the support, but I'm dropping geodjango because I need more accurate latitude and longitude. I thought it was possible using only python but it seems I'm gonna need some javascript. Nevertheless, your help was very much appreciated!