django 1.3使用clean方法表单验证来检索getlist

时间:2011-09-02 16:07:47

标签: python django

我有一个带复选框的表单,表单运行良好,在我看来我可以使用request.POST.getlist('list')来检索值列表。

目前我正在尝试在clean方法中进行一些表单验证,当我尝试使用self.cleaned_data ['list']时,我得到了最后一个值。我无法检索项目列表。

知道我怎么能这样做吗?

forms.py

class SelectList_Form(forms.Form):
    list = forms.CharField(required=False)


    def clean(self):
        super(SelectList_Form, self).clean()
        cleaned_data = self.cleaned_data

        try:
            # TODO: list validation
            if cleaned_data['list'].__len__() is 0:
                raise forms.ValidationError(_('Must select at least one of the lists below'),)

            if cleaned_data['list'].__len__() > 1:
                try:
                    # In here when i print list it only shows me the last value. It doesn't show me the list of values when the box is checked
                    print cleaned_data['list']



                except Main.DoesNotExist:
                    raise Http404

        except forms.ValidationError:
            raise

class Posting_Wizard(FormWizard):

    def render_template(self, request, form, previous_fields, step, context=None):
        if step == 0:
            obj = MainI18n.objects.filter(main__is_active=True, language=request.LANGUAGE_CODE).\
                    exclude(main__parent=None).order_by('main__parent').select_related(depth=1)
            category_choices=dict(['%s,%s' % (i.main.slug, i.main.parent.slug), '%s - %s' % (i.main.parent,i.label)] for i in obj)

            form.fields['categories'] = forms.CharField(widget=forms.RadioSelect(choices=category_choices.items()))


    if step == 1:
        category = request.POST.get('0-categories')

        pobj  = Main.objects.filter(slug=category.split(',')[1], parent=None).get()
        cobj =  Main.objects.filter(slug=category.split(',')[0], parent=pobj.id).get()
        lobj =  ListI18n.objects.filter(list__is_active=True, language=request.LANGUAGE_CODE, list__main__slug=category.split(',')[0], list__main__parent=pobj.id).select_related()

        list_choices = dict([i.id, i.title] for i in lobj)

        if cobj.mainproperties.relation == 'M':
           # Here i generate the checkboxes
            form.fields['list']=forms.CharField(widget=forms.CheckboxSelectMultiple(choices=list_choices.items()),label="Pick the list",)
        else:
            form.fields['list']=forms.CharField(widget=forms.RadioSelect(choices=list_choices.items()),label="Pick the list",)


    return super(Posting_Wizard, self).render_template(request, form, previous_fields, step, context)


def done(self, request, form_list):
    return HttpResponseRedirect(reverse('accounts-registration-wizard-done'))

def get_template(self, step):
    return 'listing/post/wizard/wizard_%s.html' % step

1 个答案:

答案 0 :(得分:0)

首先,这里有许多基本的Python错误。几乎从不需要访问双下划线函数 - 它们是内部实现细节。始终使用普通len()功能。并且,永远不要使用is进行比较:它是为了身份,所以只应该与你知道具有相同身份的事物一起使用,这基本上只是意味着None。所以你的代码应该是:

if len(cleaned_data['list']) == 0:

现在,其次,我不明白为什么你认为list中有多个'元素'。您已将其定义为CharField,它是包含许多字符的单个字段。您的len正在测试输入该字段的字符数,而不是字段数,但您认为已经定义了这些字符数。