Django表单不会绑定到POST数据

时间:2016-04-22 18:01:26

标签: django forms django-forms

我正在努力尝试将我的POST数据绑定到此表单。 POST数据正确显示并且看起来是正确的形式,但绝对拒绝绑定到表单进行验证。我是新手,所以我确定我错过了一些明显的东西,但任何帮助搞清楚为什么会发生这种情况将会非常感激。

views.py
def new_call(request):

    if not request.user.is_authenticated():
        return redirect(login_view)

    if request.method == "POST":    

        form = RegisterCallForm(request.POST,user=request.user)

        #Prints False
        print form.is_bound

        if form.is_valid():
            answered = form.cleaned_data['answered']
            left_message = form.cleaned_data['left_message']
            contact_pk = form.cleaned_data['contact']
            time_called = form.cleaned_data['time_called']
            note = form.cleaned_data['note']

            staff_person = request.user.userprofile

            call = Call(
                staff_person=staff_person,
                contact=contact,
                answered=answered,
                left_message=left_message,
                time=time_called,
                note=note,
                )

            call.save()

            if request.POST.get('submit') == 'continue':
                return redirect(user_profile_view)
            else:
                return redirect(new_call)

    else:

        form = RegisterCallForm(user=request.user,)

    return render(request, 'supporttracker/register_call.html',{'form':form})

这是形式......

forms.py
class RegisterCallForm(forms.Form):

    def __init__(self,*args,**kwargs):

        self.user = kwargs.pop('user',User)
        super (RegisterCallForm, self).__init__(*args,**kwargs)

        self.fields['contact'] = forms.TypedChoiceField(
        choices = get_contact_list(self.user),
        label = 'Call with',
        )

        new_fields = OrderedDict()

        new_fields['contact'] = self.fields['contact']
        new_fields['answered'] = self.fields['answered']
        new_fields['left_message'] = self.fields['left_message']
        new_fields['time_called'] = self.fields['time_called']
        new_fields['note'] = self.fields['note']

        self.fields = new_fields

    answered = forms.TypedChoiceField(
    label='Answered:',
    coerce=lambda x: x == 'Yes',
    choices=((False, 'No'), (True, 'Yes')),
    widget=forms.RadioSelect
    )   

    left_message = forms.TypedChoiceField(
    label='Left a message:',
    coerce=lambda x: x == 'Yes',
    choices=((False, 'No'), (True, 'Yes')),
    widget=forms.RadioSelect
    )

    time_called = forms.DateTimeField(
    label = 'Time of call',
    initial = datetime.datetime.now,
    required = False,
    )

    note = forms.CharField(
    max_length=500,
    widget=forms.Textarea,
    required=False,
    )

1 个答案:

答案 0 :(得分:1)

好像你忘了打电话给super init。当你覆盖init函数时,你怎么期望表单类做它的正常工作?