开发者

Request data from database using Django

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜