0

我目前正在开发一个应用程序,我想在该应用程序中自动将纬度和经度坐标插入用户表单。我在这里找到了一篇以前的帖子,其目的几乎完全是我试图完成对 IP 进行地理编码并使用模型表单进行保存的工作,但我似乎在这个过程中犯了一个错误。我已经安装了 GeoIP 来跟踪用户的 IP 和纬度、经度。

这是视图代码:

@login_required
def submit_story(request):
if request.method =="POST":
    story_form = StoryForm(request.POST, request.FILES)
    if story_form.is_valid():
        new_story = story_form.save(ip_address=request.META['REMOTE_ADDR'])
        new_story.author = request.user
        new_story.save()
        return HttpResponseRedirect("/report/all/")
else: # GET request
    story_form = StoryForm()
return render_to_response("report/report.html", {'form': story_form}, context_instance=RequestContext(request))

这就是我认为问题所在的表格:

class StoryForm(forms.ModelForm):
    class Meta:
        model = Story
        exclude = ('author','latitude', 'longitude')

    def save(self, ip_address, *args, **kwargs):
        g = GeoIP()
        lat, lon = g.lat_lon(ip_address)
        user_location = super(StoryForm, self).save(commit=False)
        user_location.latitude = lat
        user_location.longitude = lon
        user_location.save(*args, **kwargs)

这是回溯:

Environment:


Request Method: POST
Request URL: http://localhost:8000/report/report/

Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'registration',
 'profiles',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'stentorian.report',
 'south',
 'sorl.thumbnail',
 'gmapi',
 'django.contrib.admin']
Installed Middleware:
 ('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
     File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-        packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
   23.                 return view_func(request, *args, **kwargs)
 File "/Users/myname/Development/stentorian/../stentorian/report/views.py" in submit_story
  30.             new_story = story_form.save(ip_address=request.META['REMOTE_ADDR'])
 File "/Users/myname/Development/stentorian/../stentorian/report/forms.py" in save
  16.         lat, lon = g.lat_lon(ip_address)

Exception Type: TypeError at /report/report/
Exception Value: 'NoneType' object is not iterable

这是我第一次涉足地理定位和第一个 Django 项目,所以我很确定错误很简单(也许我需要拆分元组,不确定)。

4

2 回答 2

0

嗯...只是为了确定...您粘贴的代码中没有一些明显的缩进错误吗?你的错误到底说了什么?

编辑:我相信您的错误可能在于您通过 name 传递 ip_address,而您将其定义为表单save()方法中的位置参数。顺便说一句,您链接的另一个答案中似乎也发生了... :)

尝试在代码中更改此行:

def save(self, ip_address, *args, **kwargs)

通过这个:

def save(self, ip_address=None, *args, **kwargs)

于 2012-05-25T23:05:24.390 回答
0

lat, lon = g.lat_lon(ip_address)

导致异常,因为 g.lat_lon() 返回 None,因此无法将其结果解压缩到latandlon中。

它应该返回类似的东西(32.0434235, 43.532522)

这意味着您要么将无效参数传递给g.lat_lon(),要么由于其他原因它无法解析纬度/经度。

更新:

new_story = story_form.save(ip_address=request.META['REMOTE_ADDR'])

我想您从本地工作站运行它,在这种情况下, request.META['REMOTE_ADDR']它的价值"127.0.0.1"显然不是g.lat_lon()可以解决的。

在开发中,settings.DEBUG == True。所以你可以做以下

from django.conf import settings

def submit_story(request):
    remote_addr = request.META['REMOTE_ADDR']
    if settings.DEBUG:
        # We are in debug mode, so we set it to a dummy value.
        remote_addr = 'google.com'

    if request.method =="POST":
        story_form = StoryForm(request.POST, request.FILES)
        if story_form.is_valid():
            new_story = story_form.save(ip_address=remote_addr)
于 2012-05-25T23:56:02.687 回答