【问题标题】:Django GeoIP import [duplicate]Django GeoIP 导入 [重复]
【发布时间】:2012-10-31 23:47:54
【问题描述】:

可能重复:
Error setting up geoip on Django

我从浏览器收到“无法导入名称 GeoIP”错误,但在 python 终端上没有。例如 /tmp/geo 中的地理数据。以下在 python 终端中工作。

    from django.contrib.gis.geoip import GeoIP
    GeoIP(path='/tmp/geo/')

但是 django 视图中的以下内容给出了错误

    from django.contrib.gis.geoip import GeoIP
    return HttpResponse (GeoIP(path='/tmp/geo/'))

任何指针都会有所帮助。我正在使用 django 1.4,python 2.6。这是痕迹。谢谢。

    Traceback:
    File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
    101.                             request.path_info)
    File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
    300.                     sub_match = pattern.resolve(new_path)
    File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
    209.             return ResolverMatch(self.callback, args, kwargs, self.name)
    File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in callback
    216.         self._callback = get_callable(self._callback_str)
    File "/usr/lib/python2.6/site-packages/django/utils/functional.py" in wrapper
    27.         result = func(*args)
    File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in get_callable
    92.                 lookup_view = getattr(import_module(mod_name), func_name)
    File "/usr/lib/python2.6/site-packages/django/utils/importlib.py" in import_module
    35.     __import__(name)
    File "/x/y/z/views.py" in <module>
    12. from django.contrib.gis.utils import GeoIP

    Exception Type: ImportError at /
    Exception Value: cannot import name GeoIP

【问题讨论】:

  • 试过“from django.contrib.gis.utils.geoip import GeoIP”也不行
  • @NathanVillaescusa 我认为这不再适用于 django 1.4

标签: python django geoip


【解决方案1】:

这两个语句似乎不同(查看堆栈跟踪):

from django.contrib.gis.utils import GeoIP

对比

from django.contrib.gis.geoip import GeoIP

查看the sourceGeoIPdjango.contrib.gis.geoip.base 中定义并在django.contrib.gis.geoip 中导入,这解释了为什么它在控制台中有效,而不是在您使用django.contrib.gis.utils.GeoIP 的视图中有效。

因此,您应该在任何地方使用from django.contrib.gis.geoip import GeoIP


您的问题可能源于django.contrib.gis.utils 模块是removed in Django 1.4

【讨论】:

  • 我试过“从 django.contrib.gis.geoip 导入 GeoIP”,但没有用
  • 你可以试试from django.contrib.gis import geoip,然后是print geoip.HAS_GEOIP吗?并给出结果?
  • 我得到了正确的提示
  • @user1783848 如果您得到True,则必须定义geoip.GeoIP。您的 django 应用程序是否与提示符在同一虚拟环境中运行?
  • 你能解释一下吗。我需要在哪里以及如何定义它?
猜你喜欢
  • 2012-09-27
  • 2023-03-07
  • 1970-01-01
  • 2014-03-13
  • 2011-12-02
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 2021-02-27
相关资源
最近更新 更多