2016-08-16 69 views
-1

form.py__init __()恰恰4個參數(給定1)

class InvoiceForm(ModelForm,): 

    def __init__(self,em,first,last): 
     self.email=em 
     self.first=first 
     self.last=last 
     super(InvoiceForm,self).__init__(self,em,first,last) 
     self.fields['email']=forms.ChoiceField(choices=[x.email for x in AuthUser.objects.filter(email=em)]) 
     self.fields['first']=forms.ChoiceField(choices=[x.first_name for x in AuthUser.objects.filter(first_name=first)]) 
     self.fields['last']=forms.ChoiceField(choices=[x.last_name for x in AuthUser.objects.filter(last_name=last)]) 
    total_credits_ordered=forms.IntegerField(label=mark_safe('<br/> total_credits_ordered')) 
    total_mobile_cr_ordered=forms.IntegerField(label=mark_safe('<br/> total_mobile_cr_ordered')) 
    total_cloud_cr_ordered=forms.IntegerField(label=mark_safe('<br/> total_cloud_cr_ordered')) 
    invoice_currency=forms.CharField(label=mark_safe('<br/> invoice_currency'),max_length=100) 
    invoice_currency_code=forms.IntegerField(label=mark_safe('<br/>invoice_currency_code ')) 
    invoice_country=forms.CharField(label=mark_safe('<br/> invoice_country'),max_length=100) 
    invoice_note=forms.CharField(label=mark_safe('<br/> invoice_note'),max_length=100) 


    class Meta: 
     model=Invoices 
     fields=['total_credits_ordered','total_mobile_cr_ordered','total_cloud_cr_ordered','invoice_currency','invoice_currency_code','invoice_country','invoice_note'] 

views.py

def test(request): 
    from app.tests import model_tests 
    m = model_tests() 
    print "assf" 


    try: 

     if request.method=="POST": 
      print "sff" 
      m.create_user_types() 
      cform=CustomerForm(request.POST) 
      if cform.is_valid(): 
       em=cform.cleaned_data['email'] 
       username=email 
       password = cform.cleaned_data['password'] 
       first=cform.cleaned_data['first'] 
       last=cform.cleaned_data['last'] 
       companyname=cform.cleaned_data['company_name'] 
       companyaddr=cform.cleaned_data['company_addr'] 
       companystate=cform.cleaned_data['company_state'] 
       companycountry=cform.cleaned_data['company_country'] 
       id=m.create_customer(username,email,password,first,last,companyname,companyaddr,companystate,companycountry) 
       print "SFsfg" 
       iform=InvoiceForm(email,first,last) 
       print "ggg" 
       if iform.is_valid(): 
        tco=iform.cleaned_data['total_credits_ordered'] 
        tmco=iform.cleaned_data['total_mobile_cr_ordered'] 
        tcco=iform.cleaned_data['total_cloud_cr_ordered'] 
        ic=iform.cleaned_data['invoice_currency'] 
        icc=iform.cleaned_data['invoice_currency_code'] 
        c=iform.cleaned_data['invoice_country'] 
        inote=iform.cleaned_data['invoice_note'] 
        id_i=m.create_invoices(id,tco,tmco,tcco,ic,icc,c,inote) 
        pform=PaymentForm() 
        print "dsf" 
        pform=PaymentForm(request.POST) 
        if pform.is_valid():    
         tpm=pform.cleaned_data['total_payment_made'] 
         ps=pform.cleaned_data['payment_status'] 
         pt=pform.cleaned_data['payment_type'] 
         m.create_payment(id_i,tpm,ps,pt)  
         return HttpResponse("test successful") 
     else: 
      print "d" 
      cform=CustomerForm() 
      iform=InvoiceForm() 
      pform=PaymentForm()          
     return render(request, 'test.html', {'cform': cform,'iform':iform,'pform':pform}) 
    except Exception as e: 
     return HttpResponse("Exception : %s" %(e)) 
    return HttpResponse("Tests Successfull...") 

它表示: Exception : __init__() takes exactly 4 arguments (1 given)

但我已經通過參數表格。

+1

您確定嗎?我可以看到,從底部6行,你不 – Sayse

+1

也包括stacktrace,請不僅消息。 –

+0

[\ _ \ _ init \ _ \ _()的可能重複只需要2個參數(給出1)?](https://stackoverflow.com/questions/25805194/init-takes-exactly-2-arguments-1-給定) –

回答

1

我們不必在這個問題的堆棧跟蹤,但問題可能在這裏:

else: 
    print "d" 
    cform=CustomerForm() 
    iform=InvoiceForm() 
    pform=PaymentForm() 

在這裏,你是不傳遞任何參數來創建對象。由於實例本身始終被傳遞,有消息稱它錯過這是em,first,last

其他參數,我建議您在else部分後刪除一切,因爲它沒有任何用處或類似這樣的警告,以避免無記載錯誤:

else: 
    print("Unsupported method "+request.method) 
+0

你能告訴我怎麼做 – jersy

+0

對不起,提出愚蠢的問題 – jersy

+0

在你的窗體__init__你正在告訴表單期望4個參數self,em,first和last。所以當你聲明你的表單沒有像CustomerForm()這樣的參數時,init函數正在尋找那3件事情,因爲只有'self'被傳遞。您需要首先爲em分配一個默認值。 – jangeador

相關問題