Django inlineformset_factory渲染抛出错误

时间:2016-04-19 12:35:31

标签: django django-forms

我正在尝试使用inlinefactory构建一个包含2个模型的视图。

Models.py

class serviceinvoice(models.Model):
    user=models.ForeignKey(User,related_name='invoice')    
    invoice_number=models.PositiveIntegerField()
    #invoice_no = models.CharField(max_length = 500, default = increment_invoice_number, null = True, blank = True)
    invoice_date = models.DateField()
    invoice_receivable=models.ForeignKey(Receivables,null=True)
    total_amount=models.DecimalField(decimal_places=2,max_digits=20)
    total_amountwithtax=models.DecimalField(decimal_places=2,max_digits=20)

    def __str__(self):
        return self.invoice_number

class serviceinvoiceitems(models.Model):
    user=models.ForeignKey(settings.AUTH_USER_MODEL,related_name='serviceinvoiceitems')
    invoice_number=models.ForeignKey(serviceinvoice)
    Product=models.CharField(max_length=1000,null=True)
    UOM=models.CharField(max_length=100,null=True)
    Quantity=models.FloatField(null=True)
    Rate=models.FloatField(null=True)
    Tax_rate=models.FloatField(null=True)

    def __str__(self):
        return self.invoice_number

forms.py

createinvoiceformset = inlineformset_factory(serviceinvoice, serviceinvoiceitems,fields='__all__')

class createinvoice(forms.ModelForm):
    class Meta:
        model = serviceinvoice
        exclude = ('user',)
        widgets = {
            'invoice_date': forms.DateInput(attrs={'class':'datepicker'}),
        }

views.py

@login_required
def createinvoice(request):
    if request.method == 'POST':
        inv_form=createinvoice(data=request.POST)        
        if inv_form.is_valid():
            new_form=inv_form.save(commit=False)
            new_formset=createinvoiceformset(request.POST,instance=new_form)
            if new_formset.is_valid():
                new_form.save()
                new_formset.save()
                return HttpResponse('Invoice created')
    else:
        inv_form=createinvoice()
        new_formset=createinvoiceformset(instance=serviceinvoice())
        inv_form.fields["invoice_receivable"].queryset=Receivables.objects.filter(user=request.user)
    return render(request,'account/createinvoice.html',{'inv_form':inv_form,'new_formset':new_formset})

在模板中呈现此错误。

  

_wrapped_view()缺少1个必要的位置参数:'请求'

回溯

Environment:


Request Method: GET
Request URL: http://localhost:8000/invoice/createinvoice/

Django Version: 1.8.9
Python Version: 3.4.0
Installed Applications:
('invoice',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "D:\pythonspace\tiktant\invoice\views.py" in createinvoice
  106.         inv_form=createinvoice(instance=request)

Exception Type: TypeError at /invoice/createinvoice/
Exception Value: _wrapped_view() missing 1 required positional argument: 'request'

1 个答案:

答案 0 :(得分:1)

您的观看名称createinvoice与您的表单createinvoice发生冲突。他们应该是不同的。

如果您使用下划线表示函数名称(例如def create_invoice(...)),使用CamelCase表示类名(例如class CreateInvoice(...)CreateInvoiceFormSet,...)