简体   繁体   中英

geodjango admin map not displaying coordinate point, point is apparently saved correctly though

so I am building a program that requires me to geocode a users address and then store it in geodjango pointfield. So I have everything setup and this is my code. In my models file I have my coordinates field which is a Pointfield. However on the admin page, the map displays for the coordinates field but no point is shown. Does this mean there is an issue and things such as distance queries wont work properly? All it shows is a map with no point zoomed into the middle of no where.

The way I convert the address to coordinates is by using something called geopy. You can see in my views when saving my restaurant form, I covert the address field using the geo function:

a, c = geo.geocode(profile_address)

For example:

a, c = geo.geocode('408 Lonsdale street')
print c
(-31.812547, 149.960394)
pnt = Point(c)
print pnt
POINT (-31.8125470000000021 149.9603940000000080)

I tried other methods to covert the address to a coordinate and save it to the pointfield but it kept returning errors saying I cannot save that type of data. So I am assuming now that it saves fine, that means it should be working fine.

In my views, if I do

return HttpResponse(Restaurant.coordinates)

I get

-31.8125470000000021 149.960394

models.py

class Restaurant(models.Model):
    name = models.CharField(max_length=25, blank=False)
    user = models.ForeignKey(User)
    cuisine = models.ManyToManyField(Cuisine, blank=True)
    address = models.CharField(max_length=50, blank=False)
    coordinates = models.PointField(null=False, blank=False)
    approved = models.BooleanField(default=False)
    objects = models.GeoManager()

    def __str__(self):
        return self.name

views.py

def user_register(request):
if request.user.is_authenticated():
    return redirect('swings:profile')
if request.method == 'POST':
    user_form = UserSignUpForm(request.POST)
    restaurant_form = RestaurantForm(request.POST, request.FILES)
    if user_form.is_valid() and restaurant_form.is_valid():
        user = user_form.save(commit=False)
        user.set_password(user.password)
        user.save()
        profile = restaurant_form.save(commit=False)
        profile.user = user
        profile_address = restaurant_form.cleaned_data['address']
        a, c = geo.geocode(profile_address)
        profile_coordinates = Point(c)
        profile.coordinates = profile_coordinates
        profile.save()
        restaurant_form.save_m2m()
        #cuisines = request.POST['cuisine']
        #profile.cuisine.add(cuisine)
        return redirect('swings:login')
    else:
        return render(request, 'swings/register.html',
                      {'user_form.errors': user_form.errors, 'restaurant_form.errors': restaurant_form.errors})
else:
    user_form = UserSignUpForm()
    restaurant_form = RestaurantForm()
return render(request, 'swings/register.html', {'restaurant_form': restaurant_form, 'user_form': user_form})

forms.py

class RestaurantForm(forms.ModelForm):
class Meta:
    model = Restaurant
    exclude = ['user', 'coordinates']

admin.py and urls.py

from django.contrib.gis import admin
from swings.models import Restaurant

admin.site.register(Restaurant)

from django.contrib.gis import admin

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),

Hope I wordered my question properly. Thanks for in advance :)

The geocode method is returning the list: (latitude, longitude), but the Point constructor takes those parameters in reverse order: Point(longitude, latitude)

The call to Restaurant.coordinates is returning the Point exactly as you entered it, with the lat/long reversed, which is why it looks correct, but is plotting a random point in the middle of nowhere on the map.

This will do the trick:

a, c = geo.geocode('408 Lonsdale street')
print c
(-31.812547, 149.960394)
pnt = Point(c[1], c[0])
print pnt
POINT (149.9603940000000080, -31.8125470000000021)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM