Django using form input to query data — should be simple, isn't (for me)-Collection of common programming errors

I have a form which looks like this:

class AddressSearchForm(forms.Form):
        A form that allows a user to enter an address to be geocoded
    address = forms.CharField()

I’m not storing this value, however, I am geocoding the address and checking to make sure it is valid:

def clean_address(self):
    address = self.cleaned_data["address"]
    return geocode_address(address, True)

The geocode function looks like this:

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()
        #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')
        raise forms.ValidationError('An unknown error occured. Please try again')

    if return_text:
         address = {'text':text, 'coord':point}
        address = {'coord':point}

    return address

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?

Thanks in advance.

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:

Process should be:

1) Display form on get 2) Submit form and validate (geocode address) on post 3) Run query and display results

address = form.cleaned_data['address']
point = address['coord']
qs = model.objects.filter(point__distance_lte=(point, distance)
  1. Ok, here’s a generic version of what ended up working based on psjinx direction:

    from django.views.generic.base import TemplateResponseMixin, View
    from django.views.generic.edit import FormMixin
    from django.views.generic.list import MultipleObjectMixin
    class SearchView(FormMixin, MultipleObjectMixin, TemplateResponseMixin, View):
         A View which takes a queryset and filters it via a validated form submission
        queryset = {{ initial queryset }} # you can use a model here too eg model=foo
        form_class = {{ form }}
        def get(self, request, *args, **kwargs):
            form_class = self.get_form_class()
            form = self.get_form(form_class)
            return self.render_to_response(self.get_context_data(form=form))
        def post(self, request, *args, **kwargs):
            form_class = self.get_form_class()
            form = self.get_form(form_class)
            if form.is_valid():
                return self.form_valid(form)
                return self.form_invalid(form)    
        def form_valid(self, form):
            queryset = self.get_queryset()
            search_param = form.cleaned_data['{{ form field }}']
            object_list = queryset.filter({{ filter operation }}=search_param)
            context = self.get_context_data(object_list=object_list, form=form, search_param=search_param)
            return self.render_to_response(context) 

Originally posted 2013-11-09 21:06:49.