0

snippets of my code (views and forms)

my views.py:

 def checkout(request):
    print "signup"
    if request.method == 'POST':
        print "post payment"
        form = PaymentForm(request.POST)
        try:
            if form.is_valid():
                print form.cleaned_data
                ui.balance = ui.balance - (form.cleaned_data['amount'] * form.cleaned_data['price'])
                u.first_name = form.cleaned_data['first_name']
                u.last_name = form.cleaned_data['last_name']
                u.save()
                print "after login in signup"
                return redirect("/student/control")


            else:
                print "error"
                print form.errors
        except:
            raise
            print "error here"
            print form.errors
            pass
            #return render(request, 'student/register.html', {'form': form})

    else:
        form = PaymentForm()

    return render(request, 'student/control.html', {'form': form})

and my forms.py:

 class PaymentForm(forms.Form):
    first_name = forms.CharField(max_length = 25)
    last_name = forms.CharField( max_length = 25)
    amount = forms.IntegerField()
    price = forms.FloatField()
    def clean(self):
        cleaned_data = super(PaymentForm, self).clean()

        if User.objects.filter(first_name != cleaned_data['first_name']).count():
            raise forms.ValidationError({'first_name':['Name does not exist']})
        if User.objects.filter(last_name != cleaned_data['last_name']).count():
            raise forms.ValidationError({'last_name':['Name does not exist']})

        return cleaned_data

This problem hasn't happened before, as I used this same format for a register page and its working fine. Any suggestions?

9
  • Are you importing PaymentForm in your views.py ? Commented Nov 1, 2015 at 23:27
  • no, from forms.py @Gocht Commented Nov 1, 2015 at 23:28
  • 1
    Yes, I know PaymentForm lives in forms.py but you need to call it in views.py -> from somewhere.forms import PaymentForm Commented Nov 1, 2015 at 23:29
  • ...I'm such an idiot @Gocht Commented Nov 1, 2015 at 23:30
  • Don't say that, I am posting this as an answer, becase is a very common problem. Commented Nov 1, 2015 at 23:31

1 Answer 1

1

Keep in mind that you need to import a class to be able to use it.

In this case, you need to use PaymentForm from forms.py in your views.py. So you need to do the import:

# views.py
from somewhere.forms import PaymentForm

Now, you can use PaymentForm in views.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.