{"id":28744187,"url":"https://github.com/ipinfo/django","last_synced_at":"2025-06-16T11:11:01.283Z","repository":{"id":32949884,"uuid":"145909398","full_name":"ipinfo/django","owner":"ipinfo","description":"Official Django Library for IPinfo API (IP geolocation and other types of IP data)","archived":false,"fork":false,"pushed_at":"2025-05-08T17:28:45.000Z","size":92,"stargazers_count":57,"open_issues_count":1,"forks_count":15,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-06-13T11:55:45.007Z","etag":null,"topics":["django","ip-address","ip-database","ip-geolocation","ipinfo"],"latest_commit_sha":null,"homepage":"https://ipinfo.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ipinfo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-23T21:32:41.000Z","updated_at":"2025-05-08T17:28:49.000Z","dependencies_parsed_at":"2024-06-19T00:22:57.853Z","dependency_job_id":"abf8d9b3-d46c-4055-bf95-e605638584a1","html_url":"https://github.com/ipinfo/django","commit_stats":{"total_commits":59,"total_committers":13,"mean_commits":4.538461538461538,"dds":0.8135593220338984,"last_synced_commit":"292ff55afcc2e9f35dddcf218d0a5b277d00d071"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/ipinfo/django","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipinfo%2Fdjango","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipinfo%2Fdjango/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipinfo%2Fdjango/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipinfo%2Fdjango/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ipinfo","download_url":"https://codeload.github.com/ipinfo/django/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipinfo%2Fdjango/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260148401,"owners_count":22965915,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","ip-address","ip-database","ip-geolocation","ipinfo"],"created_at":"2025-06-16T11:11:00.519Z","updated_at":"2025-06-16T11:11:01.256Z","avatar_url":"https://github.com/ipinfo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [\u003cimg src=\"https://ipinfo.io/static/ipinfo-small.svg\" alt=\"IPinfo\" width=\"24\"/\u003e](https://ipinfo.io/) IPinfo Django Client Library\n\nThis is the official Django client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to look up your own IP address, or get any of the following details for an IP:\n\n- [IP to geolocation](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude, and longitude)\n- [IP to ASN](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting, or company)\n- [IP to Company](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)\n- [IP to Carrier](https://ipinfo.io/ip-carrier-api) (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)\n\nCheck all the data we have for your IP address [here](https://ipinfo.io/what-is-my-ip).\n\n### Getting Started\n\nYou'll need an IPinfo API access token, which you can get by signing up for a free account at [https://ipinfo.io/signup](https://ipinfo.io/signup).\n\nThe free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see [https://ipinfo.io/pricing](https://ipinfo.io/pricing)\n\n⚠️ Note: This library does not currently support our newest free API https://ipinfo.io/lite. If you’d like to use IPinfo Lite, you can call the [endpoint directly](https://ipinfo.io/developers/lite-api) using your preferred HTTP client. Developers are also welcome to contribute support for Lite by submitting a pull request.\n\n#### Installation\n\n```bash\npip install ipinfo_django\n```\n\n#### Quickstart\n\nOnce configured, `ipinfo_django` will make IP address data accessible within Django's `HttpRequest` object. The following view from the `view.py` file:\n\n```python\nfrom django.http import HttpResponse\n\n\ndef location(request):\n    response_string = 'The IP address {} is located at the coordinates {}, which is in the city {}.'.format(\n        request.ipinfo.ip,\n        request.ipinfo.loc,\n        request.ipinfo.city\n    )\n\n    return HttpResponse(response_string)\n```\n\nwill return the following as an `HttpResponse` object:\n\n```python\n'The IP address 216.239.36.21 is located at the coordinates 37.8342,-122.2900, which is in the city Emeryville.'\n```\n\nTo get the details of a user-defined IP, we will import the ipinfo package directly to the `view.py` file:\n```python\nfrom django.shortcuts import render\nfrom django.http import HttpResponse\nfrom django.conf import settings\nimport ipinfo\n\n\ndef get_ip_details(ip_address=None):\n\tipinfo_token = getattr(settings, \"IPINFO_TOKEN\", None)\n\tipinfo_settings = getattr(settings, \"IPINFO_SETTINGS\", {})\n\tip_data = ipinfo.getHandler(ipinfo_token, **ipinfo_settings)\n\tip_data = ip_data.getDetails(ip_address)\n\treturn ip_data\n\ndef location(request):\n\tip_data = get_ip_details('168.156.54.5')\n\tresponse_string = 'The IP address {} is located at the coordinates {}, which is in the city {}.'.format(ip_data.ip,ip_data.loc,ip_data.city)\n\treturn HttpResponse(response_string)\n```\n\nThe above code will print the IP details provided. We can use GET and POST methods to get the details of user-defined IP\n\n```python\n'The IP address 168.156.54.5 is located at the coordinates 47.6104,-122.2007, which is in the city Bellevue.'\n```\n\n### Setup\n\nSetup can be accomplished in three steps:\n\n1. Install with `pip`\n\n```bash\npip install ipinfo_django\n```\n\n2. Add `'ipinfo_django.middleware.IPinfoMiddleware'` to `settings.MIDDLEWARE` in `settings.py`:\n\n```python\nMIDDLEWARE = [\n  'django.middleware.security.SecurityMiddleware',\n  'django.contrib.sessions.middleware.SessionMiddleware',\n  ...\n  'ipinfo_django.middleware.IPinfoMiddleware',\n]\n```\n\n3. Optionally, configure with custom settings in `settings.py`:\n\n```python\nIPINFO_TOKEN = '123456789abc'\nIPINFO_SETTINGS = {\n  'cache_options': {\n      'ttl':30,\n      'maxsize': 128\n  },\n  'countries_file': 'custom_countries.json'\n}\nIPINFO_FILTER = lambda request: request.scheme == 'http'\nIPINFO_IP_SELECTOR = my_custom_ip_selector_implementation\n```\n\n### Async support\n\n`'ipinfo_django.middleware.IPinfoAsyncMiddleware'` can be used under ASGI. This is an async-only middleware that works only when placed in an async middleware chain, that is, a chain of Django middleware which are both async and async capable. For example:\n\n```python\nMIDDLEWARE = [\n  'package_b.middleware.ExampleSyncAndAsyncMiddleware',\n  'ipinfo_django.middleware.IPinfoAsyncMiddleware',\n  'package_a.middleware.ExampleAsyncMiddleware',\n]\n```\n\nSee [asynchronous-support](https://docs.djangoproject.com/en/4.0/topics/http/middleware/#asynchronous-support) for more.\n\n### Details Data\n\n`HttpRequest.ipinfo` is a `Details` object that contains all fields listed [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed directly.\n\n```python\n\u003e\u003e\u003e request.ipinfo.hostname\ncpe-104-175-221-247.socal.res.rr.com\n```\n\n#### Country Name\n\n`HttpRequest.ipinfo.country_name` will return the country name, as supplied by the `countries.json` file. See below for instructions on changing that file for use with non-English languages. `HttpRequest.ipinfo.country` will still return country code.\n\n```python\n\u003e\u003e\u003e request.ipinfo.country\nUS\n\u003e\u003e\u003e request.ipinfo.country_name\nUnited States\n```\n\n#### IP Address\n\n`HttpRequest.ipinfo.ip` will return an IP string.\n\n```python\n\u003e\u003e\u003e request.ipinfo.ip\n104.175.221.247\n\u003e\u003e\u003e type(request.ipinfo.ip)\n\u003cclass 'str'\u003e\n```\n\n#### Longitude and Latitude\n\n`HttpRequest.ipinfo.latitude` and `HttpRequest.ipinfo.longitude` will return latitude and longitude, respectively, as strings. `HttpRequest.ipinfo.loc` will still return a composite string of both values.\n\n```python\n\u003e\u003e\u003e request.ipinfo.loc\n34.0293,-118.3570\n\u003e\u003e\u003e request.ipinfo.latitude\n34.0293\n\u003e\u003e\u003e request.ipinfo.longitude\n-118.3570\n```\n\n#### Accessing all properties\n\n`HttpRequest.ipinfo.all` will return all details data as a dictionary.\n\n```python\n\u003e\u003e\u003e request.ipinfo.all\n{\n'asn': {  'asn': 'AS20001',\n           'domain': 'twcable.com',\n           'name': 'Time Warner Cable Internet LLC',\n           'route': '104.172.0.0/14',\n           'type': 'isp'},\n'city': 'Los Angeles',\n'company': {   'domain': 'twcable.com',\n               'name': 'Time Warner Cable Internet LLC',\n               'type': 'isp'},\n'country': 'US',\n'country_name': 'United States',\n'hostname': 'cpe-104-175-221-247.socal.res.rr.com',\n'ip': '104.175.221.247',\n'loc': '34.0293,-118.3570',\n'latitude': '34.0293',\n'longitude': '-118.3570',\n'phone': '323',\n'postal': '90016',\n'region': 'California'\n}\n```\n\n### Authentication\n\nThe IPinfo library can be authenticated with your IPinfo API token, which is set in the `settings.py` file. It also works without an authentication token, but in a more limited capacity. From `settings.py`:\n\n```python\nIPINFO_TOKEN = '123456789abc'\n```\n\n### Caching\n\nIn-memory caching of `details` data is provided by default via the `cachetools \u003chttps://cachetools.readthedocs.io/en/latest/\u003e`_ library. This uses an LRU (least recently used) cache with a TTL (time to live) by default. This means that values will be cached for the specified duration; if the cache's max size is reached, cache values will be invalidated as necessary, starting with the oldest cached value.\n\n#### Modifying cache options\n\nCache behavior can be modified by setting the `cache_options` key in `settings.IPINFO_SETTINGS`. `cache_options` is a dictionary in which the keys are keyword arguments specified in the `cachetools` library. The nesting of keyword arguments is to prevent name collisions between this library and its dependencies.\n\n- Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)\n- Default TTL: 24 hours (in seconds)\n\nFrom `settings.py`:\n\n```python\nIPINFO_SETTINGS = {\n    'cache_options': {\n        'ttl': 30,\n        'maxsize': 128\n    }\n}\n```\n\n#### Using a different cache\n\nIt's possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/ipinfo/python/blob/master/ipinfo/cache/interface.py) class and setting the `cache` value in `settings.IPINFO_SETTINGS`. FYI this is known as [the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy).\n\nFrom `settings.py`:\n\n```python\nIPINFO_SETTINGS = {'cache': my_fancy_custom_cache_object}\n```\n\n### Internationalization\n\nWhen looking up an IP address, the response object includes a `details.country_name` attribute which includes the country name based on American English. It is possible to return the country name in other languages by setting the `countries_file` keyword argument in `settings.py`.\n\nThe file must be a `.json` file with the following structure:\n\n```js\n{\n    \"BD\": \"Bangladesh\",\n    \"BE\": \"Belgium\",\n    \"BF\": \"Burkina Faso\",\n    \"BG\": \"Bulgaria\"\n    // …\n}\n```\n\nFrom `settings.py`:\n\n```python\nIPINFO_SETTINGS = {'countries_file': 'custom_countries.json'}\n```\n\n### Filtering\n\nBy default, `ipinfo_django` filters out requests that have `bot` or `spider` in the user-agent. Instead of looking up IP address data for these requests, the `HttpRequest.ipinfo` attribute is set to `None`. This is to prevent you from unnecessarily using up requests on non-user traffic. This behavior can be switched off by modifying the `settings.IPINFO_FILTER` object in `settings.py`.\n\nTo turn off filtering:\n\n```python\nIPINFO_FILTER = None\n```\n\nTo set your own filtering rules, *thereby replacing the default filter*, you can set `settings.IPINFO_FILTER` to your own, custom callable function which satisfies the following rules:\n\n- Accepts one request.\n- Returns *True to filter out, False to allow lookup*\n\nTo use your own filter rules:\n\n```python\nIPINFO_FILTER = lambda request: request.scheme == 'http'\n```\n\n### IP Selection Mechanism\n\nBy default, the IP is used by ignoring the reverse proxies depending on whether we are behind a reverse proxy or not.\n\nSince the desired IP by your system may be in other locations, the IP selection mechanism is configurable and some alternative built-in options are available.\n\n#### Using built-in IP selectors\n\n- Default IP Selector\n- Client IP Selector\n\n##### Default IP selector\n\nA [DefaultIPSelector](https://github.com/ipinfo/django/blob/master/ipinfo_django/ip_selector/default.py) object is used by default if no IP selection mechanism is provided. It selects an IP address by trying to extract it from the `X-Forwarded-For` header. It will default to the source IP on the request if the header doesn't exist.\n\nThis selector can be set explicitly by setting the `IPINFO_IP_SELECTOR` in `settings.py` file.\n\n```python\nfrom ipinfo_django.ip_selector.default import DefaultIPSelector\n\nIPINFO_IP_SELECTOR = DefaultIPSelector()\n```\n\n##### Client IP selector\n\nA [ClientIPSelector](https://github.com/ipinfo/django/blob/master/ipinfo_django/ip_selector/remote_client.py) returns the client IP address from the incoming request.\n\nThis selector can be set by setting the `IPINFO_IP_SELECTOR` in `settings.py`file.\n\n```python\nfrom ipinfo_django.ip_selector.remote_client import ClientIPSelector\n\nIPINFO_IP_SELECTOR = ClientIPSelector()\n```\n\n#### Using a custom IP selector\n\nIn case a custom IP selector is required, you may implement the [IPSelectorInterface](https://github.com/ipinfo/django/blob/master/ipinfo_django/ip_selector/interface.py) and pass the instance to `IPINFO_IP_SELECTOR` in `settings.py` file.\n\nFor example:\n\n```python\nIPINFO_IP_SELECTOR = my_custom_ip_selector_implementation\n```\n\n### Errors\n\nIf there's an error while making a request to IPinfo (e.g. your token was rate\nlimited, there was a network issue, etc.), then the traceback will be logged\nusing the built-in Python logger, and `HttpRequest.ipinfo` will be `None`.\n\n### Local development and testing\n\nTo test the project locally, install Tox in your Python virtual environment:\n\n```bash\npip install tox\n```\n\nThen, run Tox:\n\n```bash\nPYTHONPATH=. tox\n```\n\n### Other Libraries\n\nThere are official IPinfo client libraries available for many languages including PHP, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.\n\n### About IPinfo\n\nFounded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for businesses and developers.\n\n[![image](https://avatars3.githubusercontent.com/u/15721521?s=128\u0026u=7bb7dde5c4991335fb234e68a30971944abc6bf3\u0026v=4)](https://ipinfo.io/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipinfo%2Fdjango","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fipinfo%2Fdjango","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipinfo%2Fdjango/lists"}