2012-04-10 62 views
0

我得到這個錯誤,當我嘗試保存模型時,整個變量都來自POST請求。 我不明白錯誤,什麼是序列索引? 節省功能的代碼是:Django,保存模型:序列索引必須是整數,而不是'str'

try: 
    product=Product() 
    product.name=request.POST.get('name',None) 
    product.save() 
    return HttpResponse('Saved') 
except: 
    return HTTPServerError("Error during save operation") 

其中名稱是一個CharField

編輯:

的模型是:

class Product(models.Model): 

     id = models.AutoField(primary_key=True) 

     name = models.CharField(max_length=300) 

的堆棧跟蹤被印刷在JavaScript控制檯,因爲它是ajax請求。所以我得到了與console.log(response.responseText);錯誤,並且只有錯誤消息和環境變量(我認爲有一種方法可以打印所有堆棧跟蹤,但我現在不知道)。

+2

能否請您編輯您的問題,包括了''Product''模式? – Brandon 2012-04-10 14:46:45

+2

看到整個堆棧跟蹤也是非常好的 – 2012-04-10 15:02:36

+1

對於初學者,您不應該檢查form.is_valid()並使用product.name = form.cleaned_data ['name']/form.cleaned_data進行分配。 get('name')? – 2012-04-10 15:10:39

回答

0

嘗試...

try: 
    product=Product(request.POST) 
    product.save() 
    return HttpResponse('Saved') 
except: 
    return HTTPServerError("Error during save operation") 
相關問題