可能重复:
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
答案 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