提交表单时出错

时间:2013-01-30 10:41:27

标签: python python-2.7 django-forms django-1.4 django-sessions

  

可能重复:
  Updating an entry on form submit instead of saving new entry

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

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

1 个答案:

答案 0 :(得分:1)

如果task不是is,则

None仅初始化,因此如果您致电searchinventory2(request),则不会初始化任务。

您应该执行以下操作:

if id is not None:
    task = Inventory.objects.get(id = id)
else:
    task = something that can be then used