2016-08-16 85 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

相关问题