当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Django GeoIP import

问题描述:

Possible Duplicate:

Error setting up geoip on Django

I get the "cannot import name GeoIP" error from the browser but not on python terminal. for example for geodata in /tmp/geo. the following works in the python terminal.

 from django.contrib.gis.geoip import GeoIP

GeoIP(path='/tmp/geo/')

However the following in a django view gives the error

 from django.contrib.gis.geoip import GeoIP

return HttpResponse (GeoIP(path='/tmp/geo/'))

Any pointer will be helpfull. I'm using django 1.4 , python 2.6. here is the trace. Thanks.

 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

网友答案:

The two statements seem to differ (look at the stacktrace):

from django.contrib.gis.utils import GeoIP

vs

from django.contrib.gis.geoip import GeoIP

Looking at the source, GeoIP is defined in django.contrib.gis.geoip.base and imported in django.contrib.gis.geoip, which explain why it works in the console, and not in the view, where you're using django.contrib.gis.utils.GeoIP.

You should therefore use from django.contrib.gis.geoip import GeoIP everywhere.


Your problem probably arises from the fact that the django.contrib.gis.utils module was removed in Django 1.4

分享给朋友:
您可能感兴趣的文章:
随机阅读: