I’m currently working on an application where I want to automatically insert the latitude and longitude coordinates into a user form. I found a previous post here which aims to do almost exactly what I’m attempting to accomplish geocoding an IP and save using model forms but I seem to be making an error in the process. I’ve installed GeoIP to track the IP and lat, lon from the user.
Here’s the view code:
@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))
And this is the form, where I believe the issue lies:
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)
This is the traceback:
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
This is my first time wading into geolocation and first Django project, so I’m quite sure the mistake is simple (perhaps I need to split the tuple, not sure).
This
is causing the exception because g.lat_lon() is returning None, so its result
can’t be unpacked into
latandlon.It should return something like
(32.0434235, 43.532522)for example.This means that you are either passing an invalid argument to
g.lat_lon(), or thatfor some other reason it can’t resolve the lat/lon.
UPDATE:
I suppose you run this from your local workstation and in that case the
request.META['REMOTE_ADDR']has the value"127.0.0.1"which obviouslyis not something that
g.lat_lon()can resolve.When in development the settings.DEBUG == True. So you could do the following