Validation issue with my Form

Question

 I have this puzzling issue with my forms. Here is my part of my views.py handling the POST request.

elif request.method == 'POST':
        obj.BetAccountBalance += int(request.POST['BetAccountBalance'])
        
        bound_form = form_Net({'BetAccountBalance':obj.BetAccountBalance}, instance=obj)

        if bound_form.is_valid():
            new_tag = bound_form.save()
            NetObj.AccountBal -= int(request.POST['BetAccountBalance'])
            NetObj.save()
            return render(request, 'Account/Success.html', {'value':'Bet'})
            
        else:
            bound_form = form_Net(request.POST, instance=obj)
            context = {'NetObj':NetObj.AccountBal, 'form':bound_form, 'email':name}
            return render(request, template_name, context)

 

And here is my forms.py and it has a custom clean method for my IntegerField in my form

class TransferToMyBet(forms.ModelForm):
    class Meta:
        model = BetDetails
        fields = ['BetAccountBalance']

    def clean_BetAccountBalance(self):
        
        data = self.cleaned_data.get('BetAccountBalance')
        
        if data > 800:
              raise forms.ValidationError('Not Enough Money')
        else:
            return data

In my form page in the browser, when i give a value greater than 800, it raises the Validation error as expected. But when i give a value less than 800, the form page doesnt save. Infact it purely acts as if Bound_form.is_valid() is false.

 

Another thing is that, when i comment out the if statement in the forms.py custom clean method, It works perfectly fine. Does that mean that, if statement doesnt work well with an Integer custom field cleaning?

 

Guest May 25, 2017, 4:26 a.m. 0 Answer

Warning! You are not logged in.
Your answer will be published as Guest user.

Post your Answer

 Prev question