Reputation: 7273
I have a form that seems to never validate. The form is just three drop-down boxes. When the form is rendered, all of the boxes have values populated and the first is selected, so no matter what, the user cannot submit bad values, yet form.is_valid() always returns false. Please help!
The form
CLUSTER_TYPES = (
('ST', 'State'),
('CNT', 'County'),
('FCD', 'Congressional District'),
('GCC', 'Circle Clustering'),
);
MAP_VIEWS = (
('1', 'Single Map'),
('2', 'Two Maps'),
('4', 'Four Maps'),
);
class ViewDataForm (forms.Form):
def __init__ (self, sets = None, *args, **kwargs):
sets = kwargs.pop ('data_sets')
super (ViewDataForm, self).__init__ (*args, **kwargs)
processed_sets = []
for ds in sets:
processed_sets.append ((ds.id, ds.name))
self.fields['data_sets'] = forms.ChoiceField (label='Data Set', choices = processed_sets)
self.fields['clustering'] = forms.ChoiceField (label = 'Clustering',
choices = CLUSTER_TYPES)
self.fields['map_view'] = forms.ChoiceField (label = 'Map View', choices = MAP_VIEWS)
The view
def main_view (request):
# We will get a list of the data sets for this user
sets = DataSet.objects.filter (owner = request.user)
# Create the GeoJSON string object to potentially populate
json = ''
# Get a default map view
mapView = MapView.objects.filter (state = 'Ohio', mapCount = 1)
mapView = mapView[0]
# Act based on the request type
if request.method == 'POST':
form = ViewDataForm (request.POST, request.FILES, data_sets = sets)
v = form.is_valid ()
if form.is_valid ():
# Get the data set
ds = DataSet.objects.filter (id = int (form.cleaned_data['data_set']))
ds = ds[0]
# Get the county data point classifications
qs_county = DataPointClassification.objects.filter (dataset = ds,
division = form.cleaned_data['clustering'])
# Build the GeoJSON object (a feature collection)
json = ''
json += '{"type": "FeatureCollection", "features": ['
index = 0
for county in qs_county:
if index > 0:
json += ','
json += '{"type": "feature", "geometry" : '
json += county.boundary.geom_poly.geojson
json += ', "properties": {"aggData": "' + str (county.aggData) + '"}'
json += '}'
index += 1
json += ']}'
mapView = MapView.objects.filter (state = 'Ohio', mapCount = 1)
mapView = mv[0]
else:
form = ViewDataForm (data_sets = sets)
# Render the response
c = RequestContext (request,
{
'form': form,
'mapView_longitude': mapView.centerLongitude,
'mapView_latitude': mapView.centerLatitude,
'mapView_zoomLevel': mapView.zoomLevel,
'geojson': json,
'valid_was_it': v
})
return render_to_response ('main.html', c)
Upvotes: 0
Views: 303
Reputation: 599460
You have overridden the signature of the form's __init__
method so that the first positional parameter is sets
. However, when you instantiate it, you pass request.POST
as the first positional argument - so the form never gets any data, so doesn't validate.
Don't change the signature of __init__
. In fact, you have everything set up correctly so you don't need to: just remove the sets=None
from the method definition, and it should all work.
Upvotes: 2