我目前正在开发一个应用程序,我想在该应用程序中自动将纬度和经度坐标插入用户表单。我在这里找到了一篇以前的帖子,其目的几乎完全是我试图完成对 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 项目,所以我很确定错误很简单(也许我需要拆分元组,不确定)。