2011-03-28 13 views
0

결코 유효성 검사를하지 않는 것 같은 양식이 있습니다. 양식은 단지 세 개의 드롭 다운 상자입니다. 양식이 렌더링되면 모든 상자에 값이 채워지고 첫 번째 값이 선택되므로 아무리 나쁜 값을 입력해도 form.is_valid()는 항상 false를 반환합니다. 도와주세요!Django 폼 유효성 검사 문제

양식

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) 

첫 번째 위치 매개 변수가 sets 수 있도록 양식의 __init__ 방법의 서명을 재정의 뷰

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) 

답변

2

. 그러나 인스턴스를 만들 때 request.POST을 첫 번째 위치 인수로 전달하므로 양식에 데이터가 전혀 입력되지 않으므로 유효성이 검사되지 않습니다.

__init__의 서명을 변경하지 마십시오. 사실, 당신은 모든 것을 정확하게 설정해야하므로, 당신은 필요하지 않습니다 : 그냥 sets=None을 메소드 정의에서 제거하면 모든 것이 작동합니다.

+0

내가주의를 기울 였다면, '세트 = 없음'이 처음에는 없었을 것입니다. 고맙습니다. – Nik