Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django表单验证问题_Django_Django Forms - Fatal编程技术网

Django表单验证问题

Django表单验证问题,django,django-forms,Django,Django Forms,我有一个表格,似乎从来没有验证过。表单只有三个下拉框。呈现表单时,所有框都填充了值,并且选择了第一个框,因此无论发生什么情况,用户都不能提交错误的值,但是form.is\u valid()始终返回false。请帮忙 形式 CLUSTER_TYPES = ( ('ST', 'State'), ('CNT', 'County'), ('FCD', 'Congressional District'), ('GCC', 'Circle Clustering'), );

我有一个表格,似乎从来没有验证过。表单只有三个下拉框。呈现表单时,所有框都填充了值,并且选择了第一个框,因此无论发生什么情况,用户都不能提交错误的值,但是form.is\u valid()始终返回false。请帮忙

形式

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)
景色

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)

您已经覆盖了表单的
\uuuu init\uuu
方法的签名,因此第一个位置参数是
。但是,当您实例化它时,您会将
request.POST
作为第一个位置参数传递-因此表单永远不会获得任何数据,因此不会进行验证


不要更改
\uuuu init\uuuu
的签名。事实上,您已经正确地设置了所有内容,因此您不需要:只需从方法定义中删除
sets=None
,它应该都能工作。

如果我一直注意的话,“sets=None”一开始就不会存在了。多谢各位。