You can use the GeoIP module included in django .
Simple middleware might look something like this:
class GeoLocationMiddleware: def process_request(self, request): if 'geoip_check' not in request.session: g = GeoIP() country = g.country(request.META.get('REMOTE_ADDR')) do_something(country)
You will notice that I am adding a flag to the session. GeoIP verification for each request is not needed and is inefficient for performance, therefore we only check it once per session. Hope this is what you were looking for.
Edit: If you want to do this only for registered users, drop there:
if request.user.is_authenticated():
at the beginning.
source share