{"title":"用 GeoIP2 进行地理定位","version":"6.1","locale":"zh-hans","docname":"ref/contrib/gis/geoip2","url":"/zh-hans/6.1/ref/contrib/gis/geoip2/","canonical":"https://djangodocs.dev/zh-hans/6.1/ref/contrib/gis/geoip2/","summary":"GeoIP2 对象是 MaxMind geoip2 Python 库的包装器。为了执行基于 IP 的地理定位，GeoIP2 对象需要 geoip2 Python 包以及以二进制格式提供的 GeoIP Country 和/或 City…","html":"<span id=\"geolocation-with-geoip2\"></span><h1>用 GeoIP2 进行地理定位<a class=\"heading-anchor\" href=\"#module-django.contrib.gis.geoip2\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"overview\">\n<span id=\"geoip2-overview\"></span><h2>概况<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"#django.contrib.gis.geoip2.GeoIP2\" title=\"django.contrib.gis.geoip2.GeoIP2\"><code class=\"xref py py-class docutils literal notranslate\">GeoIP2</code></a> 对象是 <a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/geoip2/\">MaxMind geoip2 Python 库</a> 的包装器。<a class=\"footnote-reference brackets\" href=\"#id5\" id=\"id1\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>1<span class=\"fn-bracket\">]</span></a></p>\n<p>为了执行基于 IP 的地理定位，<a class=\"reference internal\" href=\"#django.contrib.gis.geoip2.GeoIP2\" title=\"django.contrib.gis.geoip2.GeoIP2\"><code class=\"xref py py-class docutils literal notranslate\">GeoIP2</code></a> 对象需要 <a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/geoip2/\">geoip2</a> Python 包以及以二进制格式提供的 GeoIP <code class=\"docutils literal notranslate\">Country</code> 和/或 <code class=\"docutils literal notranslate\">City</code> 数据集（CSV 文件将不起作用！），可以从 <a class=\"reference external\" href=\"https://dev.maxmind.com/geoip/geolite2-free-geolocation-data\">MaxMind</a> 或 <a class=\"reference external\" href=\"https://db-ip.com/db/lite.php\">DB-IP</a> 等网站下载。获取 <code class=\"docutils literal notranslate\">GeoLite2-Country.mmdb.gz</code> 和 <code class=\"docutils literal notranslate\">GeoLite2-City.mmdb.gz</code> 文件，并将它们解压缩到与 <a class=\"reference internal\" href=\"#std-setting-GEOIP_PATH\"><code class=\"xref std std-setting docutils literal notranslate\">GEOIP_PATH</code></a> 设置相对应的目录中。</p>\n<p>另外，建议安装 <a class=\"reference external\" href=\"https://github.com/maxmind/libmaxminddb/\">libmaxminddb C 库</a> ，这样 <code class=\"docutils literal notranslate\">geoip2</code> 就可以利用 C 库更快的速度。</p>\n</section>\n<section id=\"example\">\n<h2>例如<a class=\"heading-anchor\" href=\"#example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>以下是其用法的示例：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.gis.geoip2</span> <span class=\"kn\">import</span> GeoIP2\n<span class=\"gp\">&gt;&gt;&gt; </span>g <span class=\"o\">=</span> GeoIP2<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>g<span class=\"o\">.</span>country<span class=\"p\">(</span><span class=\"s2\">&quot;google.com&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">{&#39;continent_code&#39;: &#39;NA&#39;,</span>\n<span class=\"go\"> &#39;continent_name&#39;: &#39;North America&#39;,</span>\n<span class=\"go\"> &#39;country_code&#39;: &#39;US&#39;,</span>\n<span class=\"go\"> &#39;country_name&#39;: &#39;United States&#39;,</span>\n<span class=\"go\"> &#39;is_in_european_union&#39;: False}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>g<span class=\"o\">.</span>city<span class=\"p\">(</span><span class=\"s2\">&quot;72.14.207.99&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">{&#39;accuracy_radius&#39;: 1000,</span>\n<span class=\"go\"> &#39;city&#39;: &#39;Mountain View&#39;,</span>\n<span class=\"go\"> &#39;continent_code&#39;: &#39;NA&#39;,</span>\n<span class=\"go\"> &#39;continent_name&#39;: &#39;North America&#39;,</span>\n<span class=\"go\"> &#39;country_code&#39;: &#39;US&#39;,</span>\n<span class=\"go\"> &#39;country_name&#39;: &#39;United States&#39;,</span>\n<span class=\"go\"> &#39;is_in_european_union&#39;: False,</span>\n<span class=\"go\"> &#39;latitude&#39;: 37.419200897216797,</span>\n<span class=\"go\"> &#39;longitude&#39;: -122.05740356445312,</span>\n<span class=\"go\"> &#39;metro_code&#39;: 807,</span>\n<span class=\"go\"> &#39;postal_code&#39;: &#39;94043&#39;,</span>\n<span class=\"go\"> &#39;region_code&#39;: &#39;CA&#39;,</span>\n<span class=\"go\"> &#39;region_name&#39;: &#39;California&#39;,</span>\n<span class=\"go\"> &#39;time_zone&#39;: &#39;America/Los_Angeles&#39;,</span>\n<span class=\"go\"> &#39;dma_code&#39;: 807,</span>\n<span class=\"go\"> &#39;region&#39;: &#39;CA&#39;}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>g<span class=\"o\">.</span>lat_lon<span class=\"p\">(</span><span class=\"s2\">&quot;salon.com&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">(39.0437, -77.4875)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>g<span class=\"o\">.</span>lon_lat<span class=\"p\">(</span><span class=\"s2\">&quot;uh.edu&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">(-95.4342, 29.834)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>g<span class=\"o\">.</span>geos<span class=\"p\">(</span><span class=\"s2\">&quot;24.124.1.80&quot;</span><span class=\"p\">)</span><span class=\"o\">.</span>wkt\n<span class=\"go\">&#39;POINT (-97 38)&#39;</span>\n</code></pre></div>\n</section>\n<section id=\"api-reference\">\n<h2>API 参考<a class=\"heading-anchor\" href=\"#api-reference\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GeoIP2</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">cache<span class=\"o\">=</span><span class=\"default_value\">0</span></em>, <em class=\"sig-param\">country<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">city<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\">GeoIP</code> 对象不需要任何参数来使用默认设置。然而，至少 <a class=\"reference internal\" href=\"#std-setting-GEOIP_PATH\"><code class=\"xref std std-setting docutils literal notranslate\">GEOIP_PATH</code></a> 配置应该用你 GeoIP 数据集位置的路径来设置。以下初始化关键字可用于自定义任何默认配置。</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>关键字参数</p></th>\n<th class=\"head\"><p>描述</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">path</code></p></td>\n<td><p>GeoIP 数据所在的基本目录或城市或国家数据文件（<code class=\"docutils literal notranslate\">.mmdb</code>）所在的完整路径。假设城市和国家数据集都在这个目录中；覆盖 <a class=\"reference internal\" href=\"#std-setting-GEOIP_PATH\"><code class=\"xref std std-setting docutils literal notranslate\">GEOIP_PATH</code></a> 配置。</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">cache</code></p></td>\n<td><p>打开 GeoIP 数据集时的缓存配置。可以是（0、1、2、4、8）的整数，分别对应于 <code class=\"docutils literal notranslate\">MODE_AUTO</code>、<code class=\"docutils literal notranslate\">MODE_MMAP_EXT</code>、<code class=\"docutils literal notranslate\">MODE_MMAP</code>、<code class=\"docutils literal notranslate\">GEOIP_INDEX_CACHE</code> 和 <code class=\"docutils literal notranslate\">MODE_MEMORY</code> C API 配置。默认值为 0（<code class=\"docutils literal notranslate\">MODE_AUTO</code>）。</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">country</code></p></td>\n<td><p>GeoIP 国家数据文件的名称。默认值为 <code class=\"docutils literal notranslate\">GeoLite2-Country.mmdb</code>。设置这个关键字会覆盖 <a class=\"reference internal\" href=\"#std-setting-GEOIP_COUNTRY\"><code class=\"xref std std-setting docutils literal notranslate\">GEOIP_COUNTRY</code></a> 的配置。</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">city</code></p></td>\n<td><p>GeoIP 城市数据文件的名称。默认为 <code class=\"docutils literal notranslate\">GeoLite2-City.mmdb</code>。设置这个关键字会覆盖 <a class=\"reference internal\" href=\"#std-setting-GEOIP_CITY\"><code class=\"xref std std-setting docutils literal notranslate\">GEOIP_CITY</code></a> 的配置。</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n<section id=\"methods\">\n<h2>方法<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"querying\">\n<h3>查询<a class=\"heading-anchor\" href=\"#querying\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>以下所有查询例程都可以接受 <a class=\"reference external\" href=\"https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">IPv4Address</code></a> 或 <a class=\"reference external\" href=\"https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv6Address\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">IPv6Address</code></a> 的实例、字符串 IP 地址或完全限定域名 (FQDN)。例如，<code class=\"docutils literal notranslate\">IPv4Address(&quot;205.186.163.125&quot;)</code>、<code class=\"docutils literal notranslate\">&quot;205.186.163.125&quot;</code> 和 <code class=\"docutils literal notranslate\">&quot;djangoproject.com&quot;</code> 都是有效的查询参数。</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.city\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">city</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.city\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回给定查询的城市信息字典。字典中的一些值可能是未定义的（<code class=\"docutils literal notranslate\">None</code>）。</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.country\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">country</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.country\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回给定查询的国家代码和国家的字典。</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.country_code\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">country_code</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.country_code\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回与查询对应的国家代码。</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.country_name\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">country_name</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.country_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回与查询对应的国家名称。</p>\n</section>\n<section id=\"coordinate-retrieval\">\n<h3>坐标检索<a class=\"heading-anchor\" href=\"#coordinate-retrieval\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.lon_lat\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">lon_lat</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.lon_lat\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回 (经度, 纬度) 的坐标元组。</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.lat_lon\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">lat_lon</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.lat_lon\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回 (经度, 纬度) 的坐标元组，</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2.geos\">\n<span class=\"sig-prename descclassname\">GeoIP2.</span><span class=\"sig-name descname\">geos</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">query</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2.geos\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>返回与查询对应的 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.Point\" title=\"django.contrib.gis.geos.Point\"><code class=\"xref py py-class docutils literal notranslate\">Point</code></a> 对象。</p>\n</section>\n</section>\n<section id=\"settings\">\n<h2>配置<a class=\"heading-anchor\" href=\"#settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"geoip-path\">\n<span id=\"std-setting-GEOIP_PATH\"></span><h3><code class=\"docutils literal notranslate\">GEOIP_PATH</code><a class=\"heading-anchor\" href=\"#geoip-path\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>字符串或 <a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.Path\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">pathlib.Path</code></a> 指定 GeoIP 数据文件所在的目录。除非在初始化 <a class=\"reference internal\" href=\"#django.contrib.gis.geoip2.GeoIP2\" title=\"django.contrib.gis.geoip2.GeoIP2\"><code class=\"xref py py-class docutils literal notranslate\">GeoIP2</code></a> 对象时用 <code class=\"docutils literal notranslate\">path</code> 关键字手动指定，否则该配置是 <em>必须的</em>。</p>\n</section>\n<section id=\"geoip-country\">\n<span id=\"std-setting-GEOIP_COUNTRY\"></span><h3><code class=\"docutils literal notranslate\">GEOIP_COUNTRY</code><a class=\"heading-anchor\" href=\"#geoip-country\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>GeoIP 国家数据文件的基名。默认为 <code class=\"docutils literal notranslate\">'GeoLite2-Country.mmdb'</code>。</p>\n</section>\n<section id=\"geoip-city\">\n<span id=\"std-setting-GEOIP_CITY\"></span><h3><code class=\"docutils literal notranslate\">GEOIP_CITY</code><a class=\"heading-anchor\" href=\"#geoip-city\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>GeoIP 城市数据文件的基名。默认为 <code class=\"docutils literal notranslate\">'GeoLite2-City.mmdb'</code>。</p>\n</section>\n</section>\n<section id=\"exceptions\">\n<h2>异常<a class=\"heading-anchor\" href=\"#exceptions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py exception\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.geoip2.GeoIP2Exception\">\n<em class=\"property\"><span class=\"k\">exception</span> </em><span class=\"sig-name descname\">GeoIP2Exception</span><a class=\"heading-anchor\" href=\"#django.contrib.gis.geoip2.GeoIP2Exception\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>当 <a class=\"reference internal\" href=\"#django.contrib.gis.geoip2.GeoIP2\" title=\"django.contrib.gis.geoip2.GeoIP2\"><code class=\"xref py py-class docutils literal notranslate\">GeoIP2</code></a> 包装器中发生错误时引发的异常。来自底层 <code class=\"docutils literal notranslate\">geoip2</code> 库的异常会原样传递。</p>\n</dd></dl>\n\n<p class=\"rubric\">脚注</p>\n<aside class=\"footnote-list brackets\">\n<aside class=\"footnote brackets\" id=\"id5\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id1\">1</a><span class=\"fn-bracket\">]</span></span>\n<p>GeoIP(R) 是 MaxMind, Inc 的注册商标。</p>\n</aside>\n</aside>\n</section>","rootId":"module-django.contrib.gis.geoip2","toc":[{"title":"概况","anchor":"overview","children":[]},{"title":"例如","anchor":"example","children":[]},{"title":"API 参考","anchor":"api-reference","children":[]},{"title":"方法","anchor":"methods","children":[{"title":"查询","anchor":"querying","children":[]},{"title":"坐标检索","anchor":"coordinate-retrieval","children":[]}]},{"title":"配置","anchor":"settings","children":[{"title":"GEOIP_PATH","anchor":"geoip-path","children":[]},{"title":"GEOIP_COUNTRY","anchor":"geoip-country","children":[]},{"title":"GEOIP_CITY","anchor":"geoip-city","children":[]}]},{"title":"异常","anchor":"exceptions","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/6.1/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/6.1/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/zh-hans/6.1/ref/contrib/gis/"}],"prev":{"docname":"ref/contrib/gis/gdal","title":"GDAL API","url":"/zh-hans/6.1/ref/contrib/gis/gdal/"},"next":{"docname":"ref/contrib/gis/utils","title":"GeoDjango 实用程序","url":"/zh-hans/6.1/ref/contrib/gis/utils/"},"formats":{"html":"/zh-hans/6.1/ref/contrib/gis/geoip2/","markdown":"/zh-hans/6.1/ref/contrib/gis/geoip2.md","json":"/zh-hans/6.1/ref/contrib/gis/geoip2.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/contrib/gis/geoip2.txt","official":"https://docs.djangoproject.com/zh-hans/6.1/ref/contrib/gis/geoip2/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}