title: Django using form input to query data -- should be simple, isn't (for me)
body: <p>I have a form which looks like this:</p>
  
  <pre><code>class AddressSearchForm(forms.Form):
      """
          A form that allows a user to enter an address to be geocoded
      """
      address = forms.CharField()
  </code></pre>
  
  <p>I'm not storing this value, however, I am geocoding the address and checking to make sure it is valid:</p>
  
  <pre><code>def clean_address(self):
      address = self.cleaned_data["address"]
      return geocode_address(address, True)
  </code></pre>
  
  <p>The geocode function looks like this:</p>
  
  <pre><code>def geocode_address(address, return_text = False):
      """ returns GeoDjango Point object for given address
          if return_text is true, it'll return a dictionary: {text, coord}
          otherwise it returns {coord}
      """ 
      g = geocoders.Google()
      try:
          #TODO: not really replace, geocode should use unicode strings
          address = address.encode('ascii', 'replace')            
          text, (lat,lon) = g.geocode(address)
          point = Point(lon,lat)
     except (GQueryError):
         raise forms.ValidationError('Please enter a valid address')
      except (GeocoderResultError, GBadKeyError, GTooManyQueriesError):
      raise forms.ValidationError('There was an error geocoding your address. Please try again')
      except:
          raise forms.ValidationError('An unknown error occured. Please try again')
  
      if return_text:
           address = {'text':text, 'coord':point}
      else:
          address = {'coord':point}
  
      return address
  </code></pre>
  
  <p>What I now need to do is create a view that will query a model using the address data to filter the results. I'm having trouble figuring out how to do this. I'd like to use CBV's if possible. I can use FormView to display the form, and ListView to display the query results, but how do I pass the form data between the two?</p>
  
  <p>Thanks in advance.</p>
  
  <p>UPDATE: I know how to query my model to filter the results. I just don't know how to properly combine using a Form and Class Based Views so that I can access the cleaned_data for my filter. e.g:</p>
  
  <p>Process should be:</p>
  
  <p>1) Display form on get
  2) Submit form and validate (geocode address) on post
  3) Run query and display results</p>
  
  <pre><code>address = form.cleaned_data['address']
  point = address['coord']
  qs = model.objects.filter(point__distance_lte=(point, distance)
  </code></pre>