我的代码片段(观点和表单)
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})
和我的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
之前没有发生过这个问题 我对注册页面使用了相同的格式,并且工作正常。有什么建议吗?
答案 0 :(得分:1)
请记住,您需要导入一个类才能使用它。
在这种情况下,您需要在PaymentForm
中使用forms.py
中的views.py
。所以你需要进行导入:
# views.py
from somewhere.forms import PaymentForm
现在,您可以在PaymentForm
views.py