Django表单的clean方法不会引发ValidationError,而是使is_valid()函数返回False

时间:2018-12-20 17:52:54

标签: python django validation exception

我创建了这种表格,其中有两个字段,一个字段是金额,另一个字段是帐户(贷方)中的余额。

forms.py

class SendCredits(forms.Form):
    credits = forms.IntegerField(required=False, widget=forms.HiddenInput())
    amount = forms.IntegerField(max_value=1000)


    def clean_amount(self):
        cleaned_data = super(SendCredits, self).clean()
        if cleaned_data['amount'] > cleaned_data['credits']:
            raise forms.ValidationError("Not enough credits")
        return cleaned_data

views.py

if request.method == "POST":

        acceptor_points = all_users.filter(email=request.POST.get('towards', False))[0].credit_points
        giver_points = all_users.filter(email=request.GET['email'])[0].credit_points
        amt = int(request.POST['amount'])
        form = SendCredits({'amount': request.POST.get('amount', 0), 'credits': giver_points})
        if form.is_valid():
            print("Foo!")



        else:
            print(form.non_field_errors)

当我检查控制台时,它总是会打印出其他内容:is_valid()条件的一部分。就是:

<bound method BaseForm.non_field_errors of <SendCredits bound=True, valid=False, fields=(credits;amount)>>

sendto.html

<form  class="form-group" method="POST">
                <h2>
                    Please select user to transfer funds to:
                </h2>
                {%csrf_token%}

                <table class="table table-dark">
                    <thead >
                        <th scope="col">
                            Select
                        </th>
                        <th scope="col">
                            Name
                        </th>
                        <th scope="col">
                            E-mail
                        </th>
                        <th scope="col">
                            Credit Points
                        </th>
                    </thead>
                    <tbody>
                        {%for ex in others%}
                        <tr>

                            <td>
                                <label for="towards">
                                <input type="radio" name="towards" value="{{ex.email}}"></label>
                            </td>
                            <td>
                                {{ex.user_name}}
                            </td>
                            <td>
                                {{ex.email}}
                            </td>
                            <td>
                                {{ex.credit_points}}
                            </td>
                            </label>
                        </tr>
                        {%endfor%}
                    </tbody>
                </table>
                {%for field in form%}
                <p>
                    {%if field.is_hidden%}
                    {%else%}
                    {{field.non_field_errors}}
                    {{field.label}} {{field}}
                    {%endif%}
                </p>
                {%endfor%}
                <input type="submit" value="Send Credits" class="btn btn-lg btn-primary">

            </form>

如果余额(贷项)低于输入的金额,我想提出错误。有更好的方法吗?我应该以其他方式处理吗?

1 个答案:

答案 0 :(得分:0)

那是应该发生的。实际显示异常是没有意义的。

该消息向您显示non_field_errors是一种方法,因此您需要调用。在模板中,这是自动发生的。