我从浏览器中获取“无法导入名称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
答案 0 :(得分:4)
这两个陈述似乎有所不同(看看堆栈跟踪):
from django.contrib.gis.utils import GeoIP
vs
from django.contrib.gis.geoip import GeoIP
查看the source,GeoIP
中定义django.contrib.gis.utils.GeoIP
并在django.contrib.gis.geoip.base
中导入,这解释了它在控制台中的工作原理,而不是在视图中, '正在使用from django.contrib.gis.geoip import GeoIP
。
因此,您应该在任何地方使用django.contrib.gis.utils
。
您的问题可能源于{{1}}模块django.contrib.gis.geoip