开发者

Request data from database using Django

开发者 https://www.devze.com 2022-12-08 03:40 出处:网络
I have somehow two variables for example x and y. I have also made a model with 3 fields (longitude,latitude,name) and have it activated in mysql database. I nee开发者_Python百科d to send these two va

I have somehow two variables for example x and y. I have also made a model with 3 fields (longitude,latitude,name) and have it activated in mysql database. I nee开发者_Python百科d to send these two variables(x,y) to the django server so as to search if there is an object with longitude=x and latitude=y.If there is one i want to get back it's name.

How can i do this?


Basicly you cant ask to db if there is an object with longitude=x and latitude=y like this:

url:

url(r'^get_name_by_xy/(?P<x>\d+)/(?P<y>\d+)/$', 'get_name_by_xy', name='get_name_by_xy'),

view:

def get_name_by_xy(request, x, y):
    import YourModel
    try:
        info = YourModel.objects.filter(longitude=x, latitude=y)
        for i in info:
             print i.name
    except YourModel.DoesNotExist:
         print 'No coincidences'

there you'll get all rows with x, y longitudes and latitudes if there is 1 or more than 1 :)


try: 
    x = OurModel.objects.filter(longitude=x, latitude=y)
    for cur in x:
       print cur.name
except OurModel.DoesNotExist:
    print 'sorry, there is no record you are looking for :('

Should work :)

Of course, print will show name in console.

0

精彩评论

暂无评论...
验证码 换一张
取 消