2015-11-01 36 views
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 

这个问题之前没有发生过,因为 我在寄存器页面使用了相同的格式,并且工作正常。有什么建议么?

+0

使用PaymentForm你在你views.py进口PaymentForm? – Gocht

+0

不,从forms.py @Gocht –

+1

是的,我知道'PaymentForm'住在'forms.py'中,但是你需要在'views.py' - >'from somewhere.forms import PaymentForm'中调用它... – Gocht

回答

1

请记住,您需要导入一个类才能使用它。

在这种情况下,您需要在views.py中使用PaymentFormforms.py。所以,你需要做的进口:

# views.py 
from somewhere.forms import PaymentForm 

现在,你可以在views.py

相关问题