django test ValueError:视图没有返回HttpResponse对象。它返回了None

时间:2015-09-27 17:46:01

标签: django django-testing

我有提交数据的意见。从Web浏览器,我可以提交数据并很好地重定向。但是在测试中,我得到了错误:没有返回一个HttpResponse对象。它改为返回None。

我如何通过考试?

视图代码为:

def insert_barang_po(request, po_id=None):                                                                       
    "submit barang2 dalam PO, ditamilkan setelah insert PO"                                                      
    po = get_object_or_404(PurchaseOrder, id=po_id)                                                              
    context = {'menu_sales': True,}                                                                              
    context['po'] = {'id': po.id,}                                                                               
    if request.method == 'POST':                                                                                 
        form = InsertBarangPOForm(request.POST)                                                                  
        if form.is_valid():                                                                                      
            barang_po = BarangPurchaseOrder.objects.create(                                                      
                        po=form.cleaned_data['po'],                                                              
                        finish_good=form.cleaned_data['finish_good'],                                            
                        jumlah_barang=form.cleaned_data['jumlah_barang'])                                        
            return redirect(reverse('sales:insert_barang_po', args=[po.id]))                                     
    else:                                                                                                        
        form = InsertBarangPOForm(initial={'po':po,})                                                            
        context['form'] = form                                                                                   
        return render(request, 'sales/insert_barang_po.html', context)

型号代码为:

class BarangPurchaseOrder(models.Model):                                                                         
    "Menyimpan isi dari setiap PO yg diterima"                                                                   
    po = models.ForeignKey(PurchaseOrder)                                                                        
    finish_good = models.ForeignKey(FinishGood)                                                                  
    jumlah_barang = models.IntegerField()                                                                        

    def __unicode__(self):                                                                                       
        return self.finish_good.nama 

测试代码是

def test_insert_barang_po_valid_po_id(self):                                
    po = PurchaseOrder.objects.create(nomor_po='zaneti')                    
    fg = FinishGood.objects.create(nama='selesai', code='sls')              
    c = Client()                                                            
    response = c.post(reverse('sales:insert_barang_po', args=[po.id]),      
                      {'po': po,                                            
                       'finish_good': fg.id,                                
                       'jumlah_barang': '35',},                             
                       follow=True)                                          
    bpo = BarangPurchaseOrder.objects.filter(finish_good=fg) \
                                     .order_by('id').reverse()    
    self.assertEqual(bpo[0].jumlah_barang, 35) 

1 个答案:

答案 0 :(得分:0)

您的views.py并未涵盖所有案例。如果表单无效,您的方法只返回任何内容,这就是您收到此错误消息的原因。您应该有一个else语句对应if form.is_valid()语句。