0

可能重复:
在表单提交时更新条目而不是保存新条目

我正在编辑表单并提交它,但它没有得到更新,而是存储在另一个条目中。

def searchinventory2(request, id = None):
    print "sssssssssss", id

    if id is not None:
        task = Inventory.objects.get(id = id)
    else:
           pass     
    if request.method == 'POST': # If the form has been submitted...
        print "dfdf"
        form = Inventory_List(request.POST, request.FILES, instance=task)
        print "hhhhhhhhhhhhh"
        if form.is_valid():
            print "ksjhjksfh"    
            form.save()
            return task(request, 'Task #%s created successfully.' % (task.id))
    else:
        print "in else"
        form = Inventory_List(instance = task)
    return render_to_response("smartlogis/inventoryedit.html", {'form':form,})

我收到此错误:

local variable 'task' referenced before assignment

4

1 回答 1

1

taskisis not时才初始化None,因此如果您调用searchinventory2(request),则任务未初始化。

您应该执行以下操作:

if id is not None:
    task = Inventory.objects.get(id = id)
else:
    task = something that can be then used 
于 2013-01-30T10:49:26.780 回答