title: Returning nearby locations in Django
body: <p>I've got a django model that has a custom attribute called <code>LocationField</code>.</p>
  
  <pre><code>class List(models.Model):
      id = models.AutoField(primary_key=True)
      title = models.CharField(max_length=200)
      location = LocationField(blank=True, max_length=255)
  </code></pre>
  
  <p>The values in this are stored as a string of format <code>latitude, longitude</code>. From my template, I pass a url as follows: <code>/nearby?lat='+somevalue+'&amp;long='+somevalue</code></p>
  
  <p>Now, I want to return nearby entries from <code>List</code> depending on the values that are passed. </p>
  
  <p>For this I've written a views.py function as follows: </p>
  
  <pre><code>def nearby(request):
      if request.GET['lat']:
          lat = request.GET['lat']
          longitude = request.GET['long']
          first_query = Playlist.objects.filter(location__istartswith=lat)
          for f in first_query:
             l = f.index(',')
             n_string = f[l:]
  </code></pre>
  
  <p>To clarify what I've done, <code>first_query</code> returns all entries that start with the same <code>latitude</code>. However, now I also want to match the <code>longitude</code> which is why I'm running that <code>for loop</code> and searching for the index of the comma that separates <code>latitude,longitude</code> in my <code>LocationField</code>. <code>n_string</code> takes the substring of the <code>LocationField</code> and I'm planning to then match it to my <code>longitude</code> variable.  </p>
  
  <p>My question is two part: </p>
  
  <ol>
  <li>How do I generate the query for matching the latitude and how do I return it to template?</li>
  <li>How do I check, in say, an area of 2 sq.km around that area? </li>
  </ol>
  
  <p>Are there django packages for this?</p>