0
片段(視圖和表單)從表單更改現有的數據庫條目的我的代碼
我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
這個問題之前沒有發生過,因爲 我在寄存器頁面使用了相同的格式,並且工作正常。有什麼建議麼?
使用
PaymentForm
你在你views.py進口PaymentForm? – Gocht不,從forms.py @Gocht –
是的,我知道'PaymentForm'住在'forms.py'中,但是你需要在'views.py' - >'from somewhere.forms import PaymentForm'中調用它... – Gocht